Tom Seddon
Tom Seddon

Reputation: 2748

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?

Upvotes: 11

Views: 12529

Answers (2)

Fraser
Fraser

Reputation: 78280

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?

Upvotes: 18

doqtor
doqtor

Reputation: 8484

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.

Upvotes: 6

Related Questions