Compiling with cmake and include debug information

cmake version 2.8.5

I am trying to compile my project using cmake. However, when i compile I don't think I am including the debug cflags ie -ggdb -D_DEBUG. As when I try and debug there is no debub info.

Is there any problem with the CMakeLists.txt files. I have 3 of them

# Mimimum version of cmake required
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)

# Name of project
PROJECT(sdp_creator C)

# Check for correct compiler
# Using C compiler GNUCXX for c++ compiler
IF(CMAKE_COMPILER_IS_GNUCC)
  MESSAGE(STATUS "=== GCC C COMPILER DETECTED")
  SET(CMAKE_C_FLAGS "-m32 -ggdb -D_DEBUG -Wextra -Wall -Wunreachable-code -O0 -D_LARGEFILE64_SOURCE")
ENDIF(CMAKE_COMPILER_IS_GNUCC)

# Using windows compiler i.e. msvc++
IF(WIN32)
  MESSAGE(STATUS "=== MSVC COMPILER DETECTED")
ENDIF(WIN32)

# Location of directory where include files are kept
INCLUDE_DIRECTORIES($ENV{HOME}/projects/sdp_creator/src/sdp)
INCLUDE_DIRECTORIES($ENV{HOME}/projects/sdp_creator/src/apr/inc)

# Location of directory where libraries are kept
LINK_DIRECTORIES($ENV{HOME}/projects/sdp_creator/src/apr/lib)

# Add subdirectories
ADD_SUBDIRECTORY(driver)
ADD_SUBDIRECTORY(sdp)

building shared library:

# Create a shared library called libsdp from sdp.c
# NOTE: static is the default
# NOTE: the lib prefix is automatically added
ADD_LIBRARY(sdp SHARED sdp.c)

Creating executable:

# Add executable called sdp_creator from source file
ADD_EXECUTABLE(sdp_creator main.c)

# Link the sdp library and other libraries with the excutable 
#if using windows compiler add additional windows libraries
IF(WIN32)
  TARGET_LINK_LIBRARIES(sdp_creator libsdp ws2_32)
  MESSAGE(STATUS "=== Linking executable with windows libraries")
ENDIF(WIN32)

# if using gcc compiler
# NOTE: no need to add the -l prefix i.e. -lsdp, no automatically
IF(CMAKE_COMPILER_IS_GNUCC)
  TARGET_LINK_LIBRARIES(sdp_creator sdp apr-1)
  MESSAGE(STATUS "=== Linking executable with posix libraries")
ENDIF(CMAKE_COMPILER_IS_GNUCC)

Many thanks for any advice,


If you're using the "Unix Makefiles" (or any Makefile-based) generator, set the variable CMAKE_BUILD_TYPE to "Debug"

cmake -DCMAKE_BUILD_TYPE=Debug ../src

That will automatically add the right definitions and flags for your compiler. You should not have to add any flags yourself.

With multi-configuration generators, (like Visual Studio and Xcode), CMAKE_BUILD_TYPE is ignored, because the choice of whether to build a Debug or Release configuration is left up to the developer at build-time, and is not known at CMake configure time.


You can check the exact steps used in make by setting VERBOSE=1 . That will tell you if the flags were included or not.

cmake project_dir
make VERBOSE=1

You can also check the CMakeCache.txt to see what value is assigned to CMAKE_C_FLAGS variable.


你可以使用CMAKE_C_FLAGS和CMAKE_CXX_FLAGS标志和-g0 / 1/2(编译器的调试信息标志,-g2是最高的信息):

cmake ... -DCMAKE_C_FLAGS="-g2" -DCMAKE_CXX_FLAGS="-g2" ...
链接地址: http://www.djcxy.com/p/24456.html

上一篇: 如何开始使用GTest和CMake

下一篇: 用cmake编译并包含调试信息