1

I have a whole project and usually the project is build with release mode, but sometimes, I know certain lib have problem , I want to debug it and want to build this library to debug mode and other libs remain release mode, how can I do that?

Suppose the lib_foo is that lib need to build with debug mode, I tried with target_compile_option:

target_compile_option(lib_foo PRIVATE -O0 -g)

After add above line in the lib's cmake file and make with make VERBOSE=1 to build the lib_foo library, I found following compile options in the terminal,

-O3 -DNDEBUG   -O0 -g 

the -O3 -DNDEBUG flags is from the global release build compile flags, and the -O0 -g is from target_compile_option, so my lib_foo is build with debug mode with -O0 -g? But run a unittest which depends on lib_foo, the performance of the unittest is not affect, I am not sure if above is right.

Is this the right way to build a certain lib with debug mode? or is there any better way to handle this?

BruceSun
  • 144
  • 9

2 Answers2

2

Use string(REPLACE "-O3 -DNDEBUG" "-O0 -g" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS}) or string(REPLACE "-O3 -DNDEBUG" "-O0 -g" CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE}) in your lib_foo/CMakeLists.txt.

Yun Yuan
  • 36
  • 2
  • this works, but the compile flags added will propagate to the folder included in lib_foo/CMakeLists.txt, if not want that happen, can add one line in the subfolder of foo's CMakeLists.txt, like 'string(REPLACE "-O0 -g" "-O3 -DNDEBUG" CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE})' to recover and let the change only affects lib_foo – BruceSun Aug 18 '22 at 07:37
1

sometimes, I know certain lib have problem , I want to debug it and want to build this library to debug mode and other libs remain release mode, how can I do that? [...] or is there any better way to handle this?

The easiest way is to just use the RelWithDebInfo configuration instead of trying to mix configurations. This produces a moderately optimized build with debug symbols.

Alex Reinking
  • 16,724
  • 5
  • 52
  • 86
  • thanks for the answer. sometimes the -O3 option from Release or RelWithDebInfo optimised out too much info and it's not possible to use gdb to print variable values or run program step by step. – BruceSun Aug 18 '22 at 01:12
  • @BruceSun - you can override `CMAKE_CXX_FLAGS_RELWITHDEBINFO` to be something like `-Og -g3` (on gcc) instead of the default, which is `-O2 -g` (not `-O3`) – Alex Reinking Aug 18 '22 at 01:24
  • typo here ? `-Og -g3` should be `-O2 -g3`? – BruceSun Aug 18 '22 at 01:49
  • @BruceSun - no typo. `-Og` is debugger friendly optimization. Take a look at the man page. – Alex Reinking Aug 18 '22 at 01:50
  • Glad to know that, according to here https://stackoverflow.com/a/16179105/4277805 , seems with '-Og', the whole program will slow down. In my case, the run time for the whole program will be serveral hours when compiled with '-O' flag and when compiled with '-O2', it will be less than an hour, so that's why I need seprate compile flags for a certain lib, I expect this will not affect the whole run time much and also can debug easliy. – BruceSun Aug 18 '22 at 02:13