问题描述
如果您未设置 CMAKE_BUILD_TYPE
,则默认情况下不会缓存它。我应该缓存吗?例如我应该有:
If you don't set the CMAKE_BUILD_TYPE
, then, by default, it doesn't get cached. Should I cache it? e.g. should I have:
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release" )
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Type of build (Debug, Release etc." FORCE)
endif()
或仅仅是:
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release" )
endif()
还是不是很重要吗?
推荐答案
CMake缓存中的CMAKE_BUILD_TYPE
用户通常会定义通过命令行使用 cmake -D ...
。因此,这确实会生成一个缓存的条目。
CMAKE_BUILD_TYPE
in CMake's Cache
The user would normally define CMAKE_BUILD_TYPE
via the command line with cmake -D ...
. So this does generate a cached entry.
即使您不为单个配置Makefile生成器提供 CMAKE_BUILD_TYPE
,CMake也会自动(因此用户可以选择 cmake-gui
):
And even when you don't give a CMAKE_BUILD_TYPE
for single configuration makefile generators, CMake will automatically create an empty cache entry (so the user can choose in e.g. cmake-gui
):
//Choose the type of build, options are: None(CMAKE_CXX_FLAGS or
// CMAKE_C_FLAGS used) Debug Release RelWithDebInfo MinSizeRel.
CMAKE_BUILD_TYPE:STRING=
如果 CMAKE_BUILD_TYPE没有默认值
由CMake的平台/编译器定义通过 CMAKE_BUILD_TYPE_INIT
给出,例如:
If no default for CMAKE_BUILD_TYPE
is given by the CMake's Platform/Compiler definitions via CMAKE_BUILD_TYPE_INIT
like for Windows-MSVC
:
# default to Debug builds
set(CMAKE_BUILD_TYPE_INIT Debug)
为 CMAKE_BUILD_TYPE
设置默认值是的,如果要强制使用在GUI中可见的默认值,则设置缓存的变量(请参阅@Tsyvarev答案)。 Settings a Default for CMAKE_BUILD_TYPE
So yes, if you want to force a default that is visible in the GUI then set the cached variable (see @Tsyvarev answer).
我通常不强制使用缓存的默认值,如果没有给出默认值,我只是设置一个临时值。这允许例如我的项目将用作子项目。
I don't normally force the cached defaults, I just set a temporary value if none is given. This allows e.g. my project to be used as a sub-project.
但这更多取决于您的个人喜好。
But that's more a matter of your personal taste.
在我的项目中,那些默认的 CMAKE_BUILD_TYPE
检查看起来有些复杂,因为我允许更多用例:
In my project's those default CMAKE_BUILD_TYPE
checks look a little more complex, since I allow more use cases:
# Make RelWithDebInfo the default (it does e.g. add '-O2 -g -DNDEBUG' for GNU)
# If not in multi-configuration environments, no explicit build type
# is set by the user and if we are the root CMakeLists.txt file.
if (NOT CMAKE_CONFIGURATION_TYPES AND
NOT CMAKE_NO_BUILD_TYPE AND
NOT CMAKE_BUILD_TYPE AND
CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
set(CMAKE_BUILD_TYPE RelWithDebInfo)
endif()
if (CMAKE_BUILD_TYPE)
string(TOUPPER "_${CMAKE_BUILD_TYPE}" MY_PROJECT_BUILD_TYPE_EXT)
endif()
# Choose a configuration for our compiler tests
if (NOT CMAKE_CONFIGURATION_TYPES AND
NOT CMAKE_NO_BUILD_TYPE)
set(CMAKE_TRY_COMPILE_CONFIGURATION "${CMAKE_BUILD_TYPE}")
else()
set(CMAKE_TRY_COMPILE_CONFIGURATION RelWithDebInfo)
endif()
但也许我应该更新此代码以使用新的全局属性(而不是检查 CMAKE_NO_BUILD_TYPE
)。
But probably I should update this code to take the new GENERATOR_IS_MULTI_CONFIG
global property into account (instead of checking for CMAKE_NO_BUILD_TYPE
).
- 博客文章,由
- "CMake and the Default Build Type" blog post by Marcus D. Hanwell
- CMake Issue #16768: Add reliable way to detect multi-config generators
这篇关于我应该缓存CMAKE_BUILD_TYPE吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!