Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I make cmake always generate debugging symbols?

I want cmake to generate symbols in my Release builds.

I know I can generate RelWithDebInfo builds, and get symbols in those, but I want symbols in the Release builds as well. I never want a build that doesn't have symbols.

Can I do this?

like image 532
Tom Seddon Avatar asked May 29 '15 23:05

Tom Seddon


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 set CMake debug mode?

We can use CMAKE_BUILD_TYPE to set the configuration type: cd debug cmake -DCMAKE_BUILD_TYPE=Debug .. cmake --build . cd ../release cmake -DCMAKE_BUILD_TYPE=Release ..

How do I add a debug flag to CMake?

How to enable debugging with cmake? If we use cmake to build the project, we may want to enabling the debuging mode that cmake invoke gcc with the -g so that we can debug the compiled program with gdb. This can be enabled by adding the CMAKE_BUILD_TYPE parameter to cmake: cmake -DCMAKE_BUILD_TYPE=Debug .

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.


2 Answers

Just to expand on @doqtor's correct answer, you have a couple of choices. Either set the CMAKE_CXX_FLAGS globally which applies these flags to all build types:

if(MSVC)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Zi")
else()
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")
endif()

or for more fine-grained control, use target_compile_options along with generator expressions to apply the flags per-target:

target_compile_options(example_target PRIVATE
    $<$<CXX_COMPILER_ID:MSVC>:/Zi>
    $<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-g>
    )

As of CMake 3.2.2 there's no built-in CMake way to just specify "turn on debug flags". I'm not sure if that's ever going to be on the cards, since there are only so many platform-specific details CMake can abstract.

The flags you can set for debug symbols can't really be equated across the platforms; e.g. how would the difference between MSVC's Z7, Zi and ZI be expressed in a meaningful cross-platform way?

like image 62
Fraser Avatar answered Sep 19 '22 22:09

Fraser


What you want to achive doesn't make sense because you will end up having 2 targets doing basically the same, but anyway ... you can pass additional flags to the compiler this way (you don't say which compiler you use so I'm going to assume it's gcc):

SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")

For other compilers that should be similar except the switch to include debug info symbols that can be different.

like image 25
doqtor Avatar answered Sep 17 '22 22:09

doqtor