发现问题:添加自定义构建和自定义配置之间存在混淆:
- 配置适用于 Visual Studio 或 XCode 等特殊工具
- 构建类型是一个更原始的功能
因此,要添加自定义构建类型,根本不需要操作 CMAKE_CONFIGURATION_TYPES 变量。只需设置相应的变量并开始使用它,如下所述:
SET(GCC_DEBUG_FLAGS "-g -Wall")
# Add new build types
message("* Adding build types...")
SET(CMAKE_CXX_FLAGS_COVERAGE
"${GCC_DEBUG_FLAGS} -fprofile-arcs -ftest-coverage"
CACHE STRING "Flags used by the C++ compiler during coverage builds."
FORCE )
SET(CMAKE_C_FLAGS_COVERAGE
"${GCC_DEBUG_FLAGS} -fprofile-arcs -ftest-coverage"
CACHE STRING "Flags used by the C compiler during coverage builds."
FORCE )
SET(CMAKE_EXE_LINKER_FLAGS_COVERAGE
""
CACHE STRING "Flags used for linking binaries during coverage builds."
FORCE )
SET(CMAKE_SHARED_LINKER_FLAGS_COVERAGE
""
CACHE STRING "Flags used by the shared libraries linker during coverage builds."
FORCE )
MARK_AS_ADVANCED(
CMAKE_CXX_FLAGS_COVERAGE
CMAKE_C_FLAGS_COVERAGE
CMAKE_EXE_LINKER_FLAGS_COVERAGE
CMAKE_SHARED_LINKER_FLAGS_COVERAGE )
我们可能还想更新帮助字符串(顺便设置默认构建类型):
IF(NOT CMAKE_BUILD_TYPE)
SET(CMAKE_BUILD_TYPE Debug
CACHE STRING "Choose the type of build : None Debug Release RelWithDebInfo MinSizeRel Coverage."
FORCE)
ENDIF(NOT CMAKE_BUILD_TYPE)
message("* Current build type is : ${CMAKE_BUILD_TYPE}")
关于尝试从另一个构建类型继承默认选项的一些警告:
SET(CMAKE_CXX_FLAGS_COVERAGE
"${CMAKE_CXX_FLAGS_DEBUG} -fprofile-arcs -ftest-coverage"
CACHE STRING "Flags used by the C++ compiler during coverage builds."
FORCE )
这是一个 坏主意,因为每次再次获取配置时(每次更改其中的某些内容)都会不断附加额外的选项。