Compiling with cmake and include debug information

2019-04-19 06:30发布

问题:

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 i.e. -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,

回答1:

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.



回答2:

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.



回答3:

you can use the CMAKE_C_FLAGS and CMAKE_CXX_FLAGS flags with -g0/1/2 (debug information flag for the compiler. -g2 is the highest information):

cmake ... -DCMAKE_C_FLAGS="-g2" -DCMAKE_CXX_FLAGS="-g2" ...


回答4:

Another option, if unix Makefiles are used to build the project, is to set CMAKE_BUILD_TYPE in CMakeLists.txt file directly:
set(CMAKE_BUILD_TYPE Debug)

You can find more in
$ man cmakevars
Look for CMAKE_BUILD_TYPE



标签: cmake