gpt4 book ai didi

build - CMakeLists.txt 中未使用 CMAKE_BUILD_TYPE

转载 作者:IT老高 更新时间:2023-10-28 11:08:20 30 4
gpt4 key购买 nike

我在将默认构建配置设置为 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"

会有同样的效果:

Enter image description here

这是因为所有配置都是内部的(即 _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

Enter image description here

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

> 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"

Enter image description here

然后构建:

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.

关于build - CMakeLists.txt 中未使用 CMAKE_BUILD_TYPE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24460486/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com