For Cmake, can you change the release / debug compiler flags with the `add_compiler_flags ()` command?

On the man page for add_compile_options() I see no mention of how to change the flags of the Release / Debug compiler. Can you use add_compiler_options() to change the flags of the Release / Debug compiler? If so, how?

If not, the recommended canonical method for modifying the variables cmake release / debug [1], as described here ?

[1] ie set cmake variables CMAKE_ <LANG> _FLAGS_ <TYPE> (for lang c / C ++ it will be: CMAKE_CXX_FLAGS_RELEASE, CMAKE_CXX_FLAGS_DEBUG, CMAKE_C_FLAGS_RELEASE, CMAKE_C_FLAGSDEB.

+3
source share
1 answer

If you want to reuse compiler settings in several of your projects or if you need to distinguish between compiler and C ++ parameters, I would recommend the option CMAKE_C_FLAGS / CMAKE_CXX_FLAGS with a tool chain file for each of the supported compilers (see, for example, here or here ).

But if you just need some additional C ++ compiler options in your project, you can use add_compile_options() , target_compile_options() or target_compile_features() .

And yes, you can DEBUG there DEBUG and RELEASE .

Examples

  1. The add_compile_options() command accepts generator expressions :

     add_compile_options("$<$<CONFIG:DEBUG>:/MDd>") 

    or

     add_compile_options( "$<$<CONFIG:RELEASE>:-std=gnu99>" "$<$<CONFIG:DEBUG>:-std=gnu99 -g3>" ) 
  2. It is better to check the compiler identifier as well:

     add_compile_options("$<$<AND:$<CXX_COMPILER_ID:MSVC>,$<CONFIG:DEBUG>>:/MDd>") 

    or

     if (MSVC) add_compile_options("$<$<CONFIG:DEBUG>:/MDd>") endif() 
  3. Better yet, let CMake choose the right compiler options for you. Thus, you can set the CXX_STANDARD necessary for your purpose:

     set_property(TARGET tgt PROPERTY CXX_STANDARD 11) 

    or provide compiler functions with your goals using target_compile_features()

     add_library(mylib requires_constexpr.cpp) # cxx_constexpr is a usage-requirement target_compile_features(mylib PUBLIC cxx_constexpr) 

Recommendations

+6
source

Source: https://habr.com/ru/post/1271778/


All Articles