cmake忽略-D CMAKE_BUILD_TYPE = Debug

我只是想在调试模式下构建一个cmake项目来启用断言。 我尝试了以下版本:

cmake -D CMAKE_BUILD_TYPE:STRING=Debug -L ../../ cmake -DCMAKE_BUILD_TYPE:STRING=Debug -L ../../ cmake -DCMAKE_BUILD_TYPE=Debug -L ../../ 

不幸的是,没有一个具有所需的效果 – CMAKE_BUILD_TYPE设置为Debug (因此NDEBUG -flag不会传递给gcc)。

另外,我将variable_watch(CMAKE_BUILD_TYPE)添加到我的主CMakeLists.txt以检查值是否被覆盖了某处。 但是第一个输出是我主要的READ_ACCESS另外我将variable_watch(CMAKE_BUILD_TYPE)添加到我的主CMakeLists.txt ,其中的值已经是Release

有人知道为什么cmake会忽略配置吗?

我正在使用cmake版本2.8.7。

提前感谢您的帮助!

好的, fgrep -R "CMAKE_BUILD_TYPE"终于找到了我的问题。 在一些CMakeLists.txt文件中,我发现了类似的东西:

 SET( CMAKE_BUILD_TYPE Release ... FORCE ) 

这会覆盖每个用户定义的参数(因为FORCE )。

对我有用的是:

 IF( NOT CMAKE_BUILD_TYPE ) SET( CMAKE_BUILD_TYPE Release ... FORCE ) ENDIF() 

谢谢你的提示!

我认为你的配置有问题..

我在这里写了一个完整,简单的例子: https : //dl.dropboxusercontent.com/u/68798379/cmake-build-type.tar.bz2

 cmake_minimum_required (VERSION 2.8) project(playlib) message("CMAKE_BUILD_TYPE = ${CMAKE_BUILD_TYPE}") IF(CMAKE_BUILD_TYPE MATCHES Debug) message("Debug build.") ELSEIF(CMAKE_BUILD_TYPE MATCHES Release) message("Release build.") ELSE() message("Some other build type.") ENDIF() add_library(TESTLIB SHARED src/test.c) 

当你执行cmake时

 cmake -DCMAKE_BUILD_TYPE=Debug ../../ 

它给出了以下输出:

 $ ./gen-linux.sh -- The C compiler identification is GNU 4.8.2 -- The CXX compiler identification is GNU 4.8.2 -- Check for working C compiler: /usr/bin/cc -- Check for working C compiler: /usr/bin/cc -- works -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working CXX compiler: /usr/bin/c++ -- Check for working CXX compiler: /usr/bin/c++ -- works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done CMAKE_BUILD_TYPE = Debug Debug build. -- Configuring done -- Generating done -- Build files have been written to: /home/wojci/stack-overflow/cmake-build-type/build/linux 

它显示正在从命令行设置CMAKE_BUILD_TYPE,并且它在CMakeLists.txt配置中被识别。

使用您的CMake版本在系统上运行时会发生什么?