简体   繁体   English

用 CMAKE_CXX_FLAGS_DEBUG 中设置的标志覆盖 CMAKE_CXX_FLAGS 中设置的标志

[英]Overwriting flag set in CMAKE_CXX_FLAGS by flag set in CMAKE_CXX_FLAGS_DEBUG

On my system, CMake populates CMAKE_CXX_FLAGS with O2 optimization flags and and I add O0 flags for the debug configurations, such that I find this in my CMakeCache.txt:在我的系统上,CMake 使用O2优化标志填充CMAKE_CXX_FLAGS ,并且我为调试配置添加了O0标志,这样我就可以在我的 CMakeCache.txt 中找到它:

//Flags used by the CXX compiler during all build types.
CMAKE_CXX_FLAGS:STRING=-O2

//Flags used by the CXX compiler during DEBUG builds.
CMAKE_CXX_FLAGS_DEBUG:STRING=-O0

According to the documentation of CMake, the configuration dependent flags are appended.根据 CMake 的文档,附加了配置相关标志。 The issue with this seems to be, that the 02 flag appears to take precedence over the O0 flag.这个问题似乎是, 02标志似乎优先于O0标志。 Is there a way to configure CMake, possibly from cli call to it, such that it uses O0 instead?有没有办法配置 CMake,可能是从 cli 调用它,这样它使用O0代替?

This is indeed very confusing so I set up a very small test.这确实很令人困惑,所以我设置了一个非常小的测试。

$ cat CMakeLists.txt 
cmake_minimum_required( VERSION 3.0 )
project(test)
add_executable( test test.cpp )

$ cat test.cpp
int main() {
}

Then I build with default options and look at the resulting options.然后我使用默认选项构建并查看生成的选项。 Check that the resulting flags are empty.检查生成的标志是否为空。 In the command line, cmake adds "-MT -MD" for dependency checking.在命令行中,cmake 添加“-MT -MD”进行依赖检查。

$ mkdir build && cd build
$ cmake ..
$ make VERBOSE=1
/usr/bin/c++    -MD -MT ... -c /tmp/test/test.cpp
$ grep ^CMAKE_CXX_FLAGS CMakeCache.txt
CMAKE_CXX_FLAGS:STRING=
CMAKE_CXX_FLAGS_DEBUG:STRING=-g
CMAKE_CXX_FLAGS_MINSIZEREL:STRING=-Os -DNDEBUG
CMAKE_CXX_FLAGS_RELEASE:STRING=-O3 -DNDEBUG
CMAKE_CXX_FLAGS_RELWITHDEBINFO:STRING=-O2 -g -DNDEBUG

Now compiling for release mode.现在编译发布模式。 Check that the options in the cache are exactly the same but cmake is now using the release options.检查缓存中的选项是否完全相同,但 cmake 现在正在使用发布选项。

$ rm -rf * 
$ cmake -DCMAKE_BUILD_TYPE=release ..
$ make VERBOSE=1
/usr/bin/c++ -O3 -DNDEBUG -MD -MT ... -c /tmp/test/test.cpp
$ grep ^CMAKE_CXX_FLAGS CMakeCache.txt 
CMAKE_CXX_FLAGS:STRING=
CMAKE_CXX_FLAGS_DEBUG:STRING=-g
CMAKE_CXX_FLAGS_MINSIZEREL:STRING=-Os -DNDEBUG
CMAKE_CXX_FLAGS_RELEASE:STRING=-O3 -DNDEBUG
CMAKE_CXX_FLAGS_RELWITHDEBINFO:STRING=-O2 -g -DNDEBUG

Now let's change the CMAKE_CXX_FLAGS variable directly.现在让我们直接更改CMAKE_CXX_FLAGS变量。 The option has been prepended to the make command line but in the cache file everything else remains the same.该选项已添加到 make 命令行,但在缓存文件中,其他所有内容都保持不变。

$ rm -rf *
$ cmake -DCMAKE_BUILD_TYPE=release -DCMAKE_CXX_FLAGS="-O1" ..
$ make VERBOSE=1
/usr/bin/c++   -O1 -O3 -DNDEBUG -MD -MT ... -c /tmp/test/test.cpp
$ grep ^CMAKE_CXX_FLAGS CMakeCache.txt 
CMAKE_CXX_FLAGS:STRING=-O1
CMAKE_CXX_FLAGS_DEBUG:STRING=-g
CMAKE_CXX_FLAGS_MINSIZEREL:STRING=-Os -DNDEBUG
CMAKE_CXX_FLAGS_RELEASE:STRING=-O3 -DNDEBUG
CMAKE_CXX_FLAGS_RELWITHDEBINFO:STRING=-O2 -g -DNDEBUG

Let's now change the CMAKE_CXX_FLAGS_RELEASE variable directly to see what happens.现在让我们直接更改CMAKE_CXX_FLAGS_RELEASE变量,看看会发生什么。 Looks like now we were able to replace the optimization flags with our own.看起来现在我们可以用我们自己的优化标志替换了。

$ rm -rf *
$ cmake -DCMAKE_BUILD_TYPE=release -DCMAKE_CXX_FLAGS_RELEASE="-O1" ..
$ make VERBOSE=1
/usr/bin/c++   -O1 -MD -MT ... -c /tmp/test/test.cpp
$ grep ^CMAKE_CXX_FLAGS CMakeCache.txt 
CMAKE_CXX_FLAGS:STRING=
CMAKE_CXX_FLAGS_DEBUG:STRING=-g
CMAKE_CXX_FLAGS_MINSIZEREL:STRING=-Os -DNDEBUG
CMAKE_CXX_FLAGS_RELEASE:STRING=-O1
CMAKE_CXX_FLAGS_RELWITHDEBINFO:STRING=-O2 -g -DNDEBUG

cmake also provides a CMAKE_<LANGUAGE>_FLAGS_<BUILDMODE>_INIT option. cmake 还提供了CMAKE_<LANGUAGE>_FLAGS_<BUILDMODE>_INIT选项。 Let's see what is the impact.让我们看看有什么影响。

$ rm -rf * 
$ cmake -DCMAKE_BUILD_TYPE=release -DCMAKE_CXX_FLAGS_RELEASE_INIT="-O1" ..
$ make VERBOSE=1
/usr/bin/c++   -O1 -O3 -DNDEBUG -MD -MT ... -c /tmp/test/test.cpp
$ grep ^CMAKE_CXX_FLAGS CMakeCache.txt 
CMAKE_CXX_FLAGS:STRING=
CMAKE_CXX_FLAGS_DEBUG:STRING=-g
CMAKE_CXX_FLAGS_MINSIZEREL:STRING=-Os -DNDEBUG
CMAKE_CXX_FLAGS_RELEASE:STRING=-O1 -O3 -DNDEBUG
CMAKE_CXX_FLAGS_RELEASE_INIT:UNINITIALIZED=-O1
CMAKE_CXX_FLAGS_RELWITHDEBINFO:STRING=-O2 -g -DNDEBUG

So the effect of those CMAKE_CXX_FLAGS_<MODE>_INIT variables is the same as setting CMAKE_CXX_FLAGS, it prepends them to the command line, but you can configure individually for each mode - release, debug, relwithdebinfo.所以这些CMAKE_CXX_FLAGS_<MODE>_INIT变量的效果与设置 CMAKE_CXX_FLAGS 相同,它将它们添加到命令行,但您可以为每种模式单独配置 - 发布、调试、relwithdebinfo。

So to the point of your question, setting CMAKE_CXX_FLAGS_DEBUG directly should override the other default options if you compile in debug mode .因此,就您的问题而言,如果您在调试模式下编译,直接设置CMAKE_CXX_FLAGS_DEBUG应该覆盖其他默认选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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