build - CMakeLists.txt 中未使用 CMAKE_BUILD_TYPE

我在将默认构建配置设置为 Release 时遇到问题。在我的 CMakeLists.txt 文件中,我在文件顶部设置了 CMAKE_BUILD_TYPE:

#enable Release ALWAYS, configure vars
set(CMAKE_BUILD_TYPE Release)
set(EXECUTABLE_NAME "ParticleSimulator")
set(VERSION_MAJOR 0)
set(VERSION_MINOR 2)

但是在构建我的项目并打开解决方案时,我总是看到 Debug模式,这与我在 CMakeLists 文件中指定的相反。我做错了什么?

我已经查看了那里的其他一些问题,但我没有看到任何特定于该问题的内容。

CMakeLists.txt 的要点.

最佳答案

有两种类型的生成器:单配置和多配置。

单一配置

类生成器:Unix Makefiles , NMake Makefiles , MinGW Makefiles , ...

您在生成步骤中设置配置类型:

cmake -H. -B_builds/Debug -DCMAKE_BUILD_TYPE=Debug "-GUnix Makefiles"

在这种情况下,构建步骤总是 调试:

> cmake --build _builds/Debug
/usr/bin/c++ -g ...
> cmake --build _builds/Debug --config Debug # `--config` ignored
/usr/bin/c++ -g ...
> cmake --build _builds/Debug --config Release # yep, ignored
/usr/bin/c++ -g ...

多配置

IDE 生成器:Visual Studio , Xcode

CMAKE_BUILD_TYPE 在生成步骤被忽略,两者:

> cmake -H. -B_builds -DCMAKE_BUILD_TYPE=Debug "-GVisual Studio 12 2013 Win64"

> cmake -H. -B_builds -DCMAKE_BUILD_TYPE=Release "-GVisual Studio 12 2013 Win64"

会有同样的效果:

这是因为所有配置都是内部的(即 _builds/msvc-opaque/Release_builds/msvc-opaque/Debug 什么的,没关系)。您可以使用 --config 选项进行切换:

> cmake --build _builds --config Release
cl /O2 ...
> cmake --build _builds --config Debug
cl /Od ...

控制 (?)

是的,你可以。只需定义 CMAKE_CONFIGURATION_TYPES :

# Somewhere in CMakeLists.txt
message("Generated with config types: ${CMAKE_CONFIGURATION_TYPES}")

默认输出:

-- Detecting CXX compiler ABI info - done
Generated with config types: Debug;Release;MinSizeRel;RelWithDebInfo
-- Configuring done

重写它:

> cmake -H. -B_builds -DCMAKE_CONFIGURATION_TYPES="Debug;Release" "-GVisual Studio 12 2013 Win64"
-- Detecting CXX compiler ABI info - done
Generated with config types: Debug;Release
-- Configuring done

您甚至可以定义自己的配置类型:

> cmake -H. -B_builds -DCMAKE_CONFIGURATION_TYPES="Debug;MyRelease" -DCMAKE_CXX_FLAGS_MYRELEASE="/My-Rel-flag" -DCMAKE_EXE_LINKER_FLAGS_MYRELEASE="/My-Linker-flags" "-GVisual Studio 12 2013 Win64"

然后构建:

cmake --build _builds --config MyRelease

凌乱(?)

如果你知道诀窍,根本不会:) 这是如何在脚本/CI 服务器/文档的构建指令等中构建/测试配置:

> CONFIG=Debug
> cmake -H. -B_builds "-DCMAKE_BUILD_TYPE=${CONFIG}" # Set Debug to Makefile, ignored by IDE
> cmake --build _builds --config "${CONFIG}" # Build Debug in IDE, ignored by Makefile
> (cd _builds && ctest -VV -C "${CONFIG}") # Test Debug in IDE, ignored by Makefile

错误的模式

if(CMAKE_BUILD_TYPE STREQUAL Debug) # Burn it with fire!!!
set(CMAKE_BUILD_TYPE MySuperRelease) # Be ready to catch a bug from IDE user...

好一个

set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} --my-debug-flags")

很好用。

target_compile_definitions(MyTarget PUBLIC "$<$<CONFIG:Debug>:MYDEBUG_MACRO>")

谢谢! :) 你可以为一名程序员节省一天的时间。

使用 Makefile 为我工作,我很高兴...

引自一本好书 nice guy你可能知道(强调我的):

Why should you bother? People who program on a variety of systems or use a variety of compilers care a lot because if they don’t, they are forced to waste time finding and fixing obscure bugs. People who claim they don’t care about portability usually do so because they use only a single system and feel they can afford the attitude that ‘‘the language is what my compiler implements.’’ This is a narrow and shortsighted view. If your program is a success, it is likely to be ported, so someone will have to find and fix problems related to implementation dependent features. In addition, programs often need to be compiled with other compilers for the same system, and even a future release of your favorite compiler may do some things differently from the current one. It is far easier to know and limit the impact of implementation dependencies when a program is written than to try to untangle the mess afterwards.

https://stackoverflow.com/questions/24460486/

相关文章:

build - 从 make 文件中禁用 make 内置规则和变量

tfs - 如何获取 TFS 构建状态通知?

visual-studio - VS2005 : Assembly '' is

visual-studio - Visual Studio 在构建期间不断挂起

javascript - 用于版本控制和缩小 javascript 的 Maven 插件

Ant 到 Maven - 多个构建目标

eclipse - 什么是最好的 Scala 构建系统?

c# - 缺少调试目标?

java - Eclipse 无法删除错误

javascript - 我可以使用 webpack 分别生成 CSS 和 JS 吗?