Can I make cmake always generate debugging symbols?

Tom Seddon picture Tom Seddon · May 30, 2015 · Viewed 7.7k times · Source

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?

Answer

Fraser picture Fraser · May 30, 2015

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?