Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling with cmake and include debug information

Tags:

cmake

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,

like image 739
ant2009 Avatar asked Dec 09 '11 10:12

ant2009


People also ask

How do I enable debug symbols in CMake?

C++ debugging To run a C++ debugger, you need to set several flags in your build. CMake does this for you with “build types”. You can run CMake with CMAKE_BUILD_TYPE=Debug for full debugging, or RelWithDebInfo for a release build with some extra debug info.

How do I debug a CMake project?

You can also start a debug session from Solution Explorer. First, switch to CMake Targets View in the Solution Explorer window. Then, right-click on an executable and select Debug. This command automatically starts debugging the selected target based on your active configuration.

Does CMake define debug?

CMake refers to different build configurations as a Build Type. Suggested build types are values such as Debug and Release, but CMake allows any type that is supported by the build tool.

Does CMake default to debug or release?

In this answer, it says Debug is the default cmake build configuration.

How do I debug C++ code in CMake?

C++ debugging To run a C++ debugger, you need to set several flags in your build. CMake does this for you with “build types”. You can run CMake with CMAKE_BUILD_TYPE=Debug for full debugging, or RelWithDebInfo for a release build with some extra debug info.

How to make CMake use the compiler/linker properly?

Then directly run the linker or compiler command that you'll see. Try to make it work by adding necessary flags or libraries. Then figure out what to change, so CMake itself can pass correct arguments to the compiler/linker command: what to change in the system (what libraries to install, which versions, versions of CMake itself)

What is CMake_find_debug_mode?

Alternatively, CMAKE_FIND_DEBUG_MODE can be set around sections of your CMakeLists.txt to limit debug printing to a specific region. To run a C++ debugger, you need to set several flags in your build. CMake does this for you with “build types”.

How do you set GDB debug flag (-g) with CMake?

C/C++: How do you set GDB debug flag (-g) with cmake? – Bytefreaks.net Add the following line to your CMakeLists.txt file to set the compilation mode to Debug (non-optimized code with debug symbols): Add the following line to your CMakeLists.txt file to set the compilation mode to RelWithDebInfo (optimized code with debug symbols):


2 Answers

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.

like image 84
DLRdave Avatar answered Oct 25 '22 09:10

DLRdave


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.

like image 25
Shyamendra Solanki Avatar answered Oct 25 '22 09:10

Shyamendra Solanki