10

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?

Tom Seddon
  • 2,648
  • 1
  • 19
  • 28
  • What exactly is the point of doing that? If you put symbols that limits the optimizations the compiler can do and it will make the code slower, which is why people make a release version to be faster. I don't see what difference there would be between your debug and release version if you do that. – meneldal May 30 '15 at 00:28
  • I know there's a difference but if you optimize like that you either won't be able to debug everything or it will be slower because functions couldn't be inlined – meneldal May 30 '15 at 00:39
  • 2
    You can assume I don't care. I have asked the question I'm interested in having answered! (cmake may be new to me, but I have been doing this a long time. I know what to expect from combining optimisation and debug symbols.) – Tom Seddon May 30 '15 at 00:39
  • I would probably go with http://www.cmake.org/cmake/help/v3.2/command/add_compile_options.html and put the options you need to pass to your compiler. – meneldal May 30 '15 at 00:45
  • 3
    @meneldal you are wrong, generating debug info symbols in release build don't affect the optimizations and the execution speed of the program. – doqtor May 30 '15 at 08:16
  • Well some optimizations will inline functions and reorder instructions. That makes it hard to debug step by step even if you have the symbols – meneldal May 30 '15 at 09:34
  • 3
    That's true, but it's no reason to leave the symbols out! I can't think of any reason you wouldn't want symbols in every build, since there are tools such as `strip' that you can use to remove them when you prepare your distribution. – Tom Seddon May 30 '15 at 13:18
  • 2
    @meneldal Having debug symbols in release build is especially useful for crash core dump analysis. You don't normally want to debug step by step release build with symbols unless you have no other choice. – doqtor May 30 '15 at 13:45

2 Answers2

17

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?

Fraser
  • 74,704
  • 20
  • 238
  • 215
  • Are you sure there are no compiler in the world other than MSVC that signify debug info generation by something other than `-g`? – einpoklum Dec 12 '19 at 21:32
6

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.

doqtor
  • 8,414
  • 2
  • 20
  • 36
  • My intention was to support gcc, clang and VC++, since with cmake you don't have to do all this per-compiler stuff, they said :) - this does at least sound workable though so I'll accept it if nothing better comes to light... – Tom Seddon May 30 '15 at 13:23