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?
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.
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 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 .
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.
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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With