用cmake编译并包含调试信息

cmake 2.8.5版

我正在尝试使用cmake编译我的项目。 但是,当我编译我不认为我包括调试cflags即-ggdb -D_DEBUG。 因为当我尝试和调试没有debub信息。

CMakeLists.txt文件有问题吗? 我有3个

# 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)

建设共享图书馆:

# 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)

创建执行

# 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)

非常感谢您的任何建议,


如果您使用“Unix Makefiles”(或任何基于Makefile的)生成器,请将变量CMAKE_BUILD_TYPE设置为“Debug”

cmake -DCMAKE_BUILD_TYPE=Debug ../src

这会自动为您的编译器添加正确的定义和标志。 你不应该自己添加任何标志。

使用多配置生成器(如Visual Studio和Xcode)时,CMAKE_BUILD_TYPE将被忽略,因为是否构建Debug或Release配置的选择在构建时由开发人员决定,并且在CMake配置时未知。


您可以通过设置VERBOSE=1来检查make中使用的确切步骤。 这会告诉你是否包含标志。

cmake project_dir
make VERBOSE=1

您还可以检查CMakeCache.txt以查看分配给CMAKE_C_FLAGS变量的值。


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

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

上一篇: Compiling with cmake and include debug information

下一篇: Switching between GCC and Clang/LLVM using CMake