CMake CMAKE_CXX_FLAGS optimized optimization

In one of my CMakeLists.txt, I have the following instructions:

IF ( MSVC ) SET ( CMAKE_CXX_FLAGS_DEBUG "/MDd" ) ) 

After creating the MSVC 10.0 solution, optimization (/ O2) was unexpectedly turned on in the code above. I am sure I did not include it in another place.

Why is this?

0
source share
1 answer

With the code in your question, you hide the default options, including the optimization level that CMake applies.

Try adding your options with

 SET ( CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MDd" ) 

or using generator expressions and add_compile_options() :

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

But you may not need this particular parameter because /MDd already included in the default CMake flag flag setting of MSVC.

Background

If you look at CMake Windows-MSVC.cmake , you will see the following initialization settings:

 set(CMAKE_${lang}_FLAGS_DEBUG_INIT "/D_DEBUG /MDd /Zi /Ob0 /Od ${_RTC1}") 

Without changing any flags in CMakeLists.txt you will see in your CMakeCache.txt :

 //Flags used by the compiler during debug builds. CMAKE_CXX_FLAGS_DEBUG:STRING=/D_DEBUG /MDd /Zi /Ob0 /Od /RTC1 

With your code, you hide this cached variable, and you end up with only /MDd .

References

+1
source

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


All Articles