CMake CMAKE_CXX_FLAGS意外启用了优化 [英] CMake CMAKE_CXX_FLAGS enabled optimization unexpectly
问题描述
在我的CMakeLists.txt文件之一中,有以下说明:
In one of my CMakeLists.txt, I have the follow instructions:
IF ( MSVC )
SET ( CMAKE_CXX_FLAGS_DEBUG "/MDd" )
)
在创建MSVC 10.0解决方案之后,上面的代码意外地启用了优化(/ O2)。我确定我没有在其他地方启用它。
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.
为什么?
推荐答案
带有您问题中的代码您将隐藏CMake确实适用的默认参数-包括优化级别。
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" )
或使用生成器表达式和 add_compile_options()
:
add_compile_options("$<$<CONFIG:Debug>:/MDd>")
但是您可能不需要此特定选项,因为 / MDd
已经是CMake的默认MSVC调试标志设置的一部分。
But you may not need this particular option, because /MDd
is already part of CMake's default MSVC debug flag's setting.
背景
如果您查看CMake的 Windows-MSVC.cmake
,您将看到以下初始化设置:
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}")
在不更改 CMakeLists.txt
中的任何标志的情况下,您会在 CMakeCache.txt $ c $中看到c>:
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
您的代码中您隐藏了此缓存的变量,最终将只得到 / MDd
。
With your code you are hiding this cached variable and you will end up with just /MDd
.
参考
- What's the CMake syntax to set and use variables?
- cmake - Global linker flag setting (for all targets in directory)
- CMAKE - setting compile flags for libraries
- Change default value of CMAKE_CXX_FLAGS_DEBUG and friends in CMake
这篇关于CMake CMAKE_CXX_FLAGS意外启用了优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!