C语言 cmake 忽略 -D CMAKE_BUILD_TYPE=Debug
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23907679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
cmake ignores -D CMAKE_BUILD_TYPE=Debug
提问by ma0ho
I'm just trying to build a cmake project in debug-mode to enable asserts. I tried the following versions:
我只是想在调试模式下构建一个 cmake 项目来启用断言。我尝试了以下版本:
cmake -D CMAKE_BUILD_TYPE:STRING=Debug -L ../../
cmake -DCMAKE_BUILD_TYPE:STRING=Debug -L ../../
cmake -DCMAKE_BUILD_TYPE=Debug -L ../../
Unfortunately none of theese has the desired effect - that CMAKE_BUILD_TYPEis set to Debug(and therefore the NDEBUG-flag is not passed to gcc).
不幸的是,这些都没有达到预期的效果 - 即CMAKE_BUILD_TYPE设置为Debug(因此 -NDEBUG标志未传递给 gcc)。
Additionally I added variable_watch(CMAKE_BUILD_TYPE)to my main CMakeLists.txtto check whether the value get's overridden somewhere. But the first output is a READ_ACCESSin my main
Additionally I added variable_watch(CMAKE_BUILD_TYPE)to my main CMakeLists.txtand the value there already is Release.
此外,我添加variable_watch(CMAKE_BUILD_TYPE)到我的主要CMakeLists.txt检查值是否在某处被覆盖。但是第一个输出是READ_ACCESS在我的 main 另外我添加variable_watch(CMAKE_BUILD_TYPE)到我的 mainCMakeLists.txt并且已经存在的值Release.
Does someone have an idea why cmake ignores the configuration?
有人知道为什么 cmake 会忽略配置吗?
I'm using cmake version 2.8.7.
我正在使用 cmake 版本 2.8.7。
回答by ma0ho
Ok, fgrep -R "CMAKE_BUILD_TYPE"finally found the problem for me. In some CMakeLists.txt-file I found something like that:
好的,fgrep -R "CMAKE_BUILD_TYPE"终于找到我的问题了。在一些CMakeLists.txt-file 中,我发现了类似的东西:
SET( CMAKE_BUILD_TYPE Release ... FORCE )
That overrides every user defined parameters (because of the FORCE).
这会覆盖每个用户定义的参数(因为FORCE)。
What works for me is that:
对我有用的是:
IF( NOT CMAKE_BUILD_TYPE )
SET( CMAKE_BUILD_TYPE Release ... FORCE )
ENDIF()
Thank's for your hints!
谢谢你的提示!
回答by wojciii
I assume that there is something wrong with your config..
我认为您的配置有问题..
I wrote a complete, simple example here: https://dl.dropboxusercontent.com/u/68798379/cmake-build-type.tar.bz2
我在这里写了一个完整、简单的例子: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)
When you execute cmake with
当你执行 cmake 时
cmake -DCMAKE_BUILD_TYPE=Debug ../../
It gives the following output:
它提供以下输出:
$ ./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
It shows that CMAKE_BUILD_TYPE is being set from the command line and it being recognized in the CMakeLists.txt config.
它显示正在从命令行设置 CMAKE_BUILD_TYPE 并且它在 CMakeLists.txt 配置中被识别。
What happens when you run it on your system using your version of CMake?
当你使用你的 CMake 版本在你的系统上运行它时会发生什么?
回答by wierzmar
This works for me:
这对我有用:
IF(${CMAKE_BUILD_TYPE} MATCHES Debug)
...
ENDIF()

