0

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

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

After the MSVC 10.0 solution is created, the optimization (/O2) is unexpectedly enabled by the code above. I'm sure I didn't enable it somewhere else.

Why is that?

Florian
  • 39,996
  • 9
  • 133
  • 149
Shaobo Zi
  • 709
  • 1
  • 10
  • 25
  • 1
    What's the type of the build? I suppose, `Release`. You may try to explicitly select `Debug`: `-DCMAKE_BUILD_TYPE=Debug`. In this case CMake provides suitable flags. – user3159253 Oct 14 '15 at 02:49

1 Answers1

1

With the code in your question you are hiding the default parameters - including the optimization level - that CMake does apply.

Please try appending your options with

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

or with the use of generator expressions and add_compile_options():

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

But you may not need this particular option, because /MDd is already part of CMake's default MSVC debug flag's setting.

Background

If you look at CMake's Windows-MSVC.cmake you'll see the following initialization settings:

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

Without changing any flags in your 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 are hiding this cached variable and you will end up with just /MDd.

References

Community
  • 1
  • 1
Florian
  • 39,996
  • 9
  • 133
  • 149
  • That's it ! Great answer BTW. – Shaobo Zi Oct 14 '15 at 09:30
  • @ShaoboZi You are welcome. Just wanted to share another link I found useful when struggling with CMake: [A list of common CMake antipatterns](http://voices.canonical.com/jussi.pakkanen/2013/03/26/a-list-of-common-cmake-antipatterns/). – Florian Oct 15 '15 at 07:13