简体   繁体   中英

Compiler flags when setting debug and release mode

gcc (GCC) 4.7.2 cmake version 2.8.10.2 c89

I am using cmake as my build system. And I want to set the flags accordingly to either debug or release.

Compile with debug

  SET(CMAKE_C_FLAGS_DEBUG "-Wall -Wextra -Wunreachable-code -g -m32 -D_DEBUG -O0 -D_LARGEFILE64_SOURCE -D_REETRANT -D_THREAD_SAFE")

Compile with release

  SET(CMAKE_C_FLAGS_RELEASE "-Wall -Wextra -Wunreachable-code -m32 -DNDEBUG -O2 -D_LARGEFILE64_SOURCE -D_REETRANT -D_THREAD_SAFE")

For the debug I have included the flags -g and set optimization to level 0.

For the release I have removed flags -g and add the N for -DNDEBUG , -DNREETRANT , and -DNTHREAD_SAFE and set the optimization to level 2.

I am right in saying that the N means NO DEBUG ?

Is this all I would need to do to distinguish between a debug and release build?

Here is some recommendations based on your CMake flags:

  1. Debug mode -g -O0 , and release mode -O2 -g . It is better to add the -g flag even for O2 release mode if the code size is not critical. With -g , some symbol information of the code is included, it only increase the whole binary size by about 10%~ 20%, but users can help give you a stack dump, which can help a lot for debug.
  2. Use -UDEBUG -UREETRANT instead of -DNDEBUG -DNREETRANT . The -DMACRO flag is just like add #define MACOR in your code, and if you have code region like #ifdef MACRO SOME_DEBUG_CODE #endif , the preprocessor will keep the code there, while -UDEBUG flag is like #undefine MACRO , and the preprocessor will delete the SOME_DEBUG_CODE . If you just add -DNDEBUG -DNREETRANT , it is like add #define NDEBUG #defin NREETRANT in your code, which is meaningless.
  • What would you need -D_DEBUG for if you could just as well check for the absence of NDEBUG ? (Unless, of course, you want seperate control over assert and other debugging #ifdef 's. I never had that need.)

  • Why would you want different settings for reentrancy and thread safety?

Bottom line, what you want different between Debug and Release code is completely up to you. I want my debug code to include coverage information, and my release code to be -O3 . Your mileage obviously varies. Neither way is "right" or "wrong".

You might also want to note that:

  • CMake sets stuff like -g -O0 and -O2 automatically, so it would be sufficient to append the desired warnings and additional flags.

  • You might want to keep an eye on portability and check for if ( CMAKE_COMPILER_IS_GNUC ) (or ..._GNUCC for C++) before you set the compiler flags.

  • As Ling Kun said, you can keep -g even in Release code. At least during development it is useful, and you can command CMake to strip executables during installation. (Personally, I strive for not having to make my customers extract post-mortem stack / core dumps. ;-) )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM