BruceSun
BruceSun

Reputation: 166

How to build a certain library with debug mode while other library build with release mode

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?

Upvotes: 1

Views: 684

Answers (2)

Yun Yuan
Yun Yuan

Reputation: 36

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.

Upvotes: 2

Alex Reinking
Alex Reinking

Reputation: 19946

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.

Upvotes: 1

Related Questions