[英]How to enable C++17 in CMake
I'm using VS 15.3, which supports integrated CMake 3.8.我正在使用 VS 15.3,它支持集成的 CMake 3.8。 How can I target C++17 without writing flags for each specific compilers?如何在不为每个特定编译器编写标志的情况下以 C++17 为目标? My current global settings don't work:我当前的全局设置不起作用:
# https://cmake.org/cmake/help/latest/prop_tgt/CXX_STANDARD.html
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
# expected behaviour
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++latest")
I expected CMake to add "/std:c++lastest" or equivalents when generating VS solution files, but no c++17 flags was found, resulted in compiler error:我希望 CMake 在生成 VS 解决方案文件时添加“/std:c++lastest”或等效项,但没有找到 c++17 标志,导致编译器错误:
C1189 #error: class template optional is only available with C++17.
Your approach is the correct one, but it will not work for MSVC on versions of CMake prior to 3.10.您的方法是正确的,但它不适用于 3.10 之前的 CMake 版本的 MSVC。
From the CMake 3.9 documentation :来自CMake 3.9文档:
For compilers that have no notion of a standard level, such as MSVC, this has no effect.对于没有标准级别概念的编译器,例如 MSVC,这没有任何影响。
In short, CMake haven't been updated to accommodate for the standard flags added to VC++ 2017.简而言之,CMake 尚未更新以适应添加到 VC++ 2017 的标准标志。
You have to detect if VC++ 2017 (or later) is used and add the corresponding flags yourself for now.您必须检测是否使用了 VC++ 2017(或更高版本)并自己添加相应的标志。
In CMake 3.10 (and later) this have been fixed for newer version of VC++.在 CMake 3.10(及更高版本)中,这已针对较新版本的 VC++ 修复。 See the 3.10 documentation .请参阅3.10 文档。
Modern CMake propose an interface for this purpose target_compile_features
.现代 CMake 为此目的提出了一个接口target_compile_features
。 Documentation is here: Requiring Language Standards文档在这里: 需要语言标准
Use it like this:像这样使用它:
target_compile_features(${TARGET_NAME} PRIVATE cxx_std_17)
In modern CMake, I've found it best to assign CXX standards at the target level instead of global variable level and use the built in properties (seen here: https://cmake.org/cmake/help/latest/manual/cmake-properties.7.html ) to keep it compiler agnostic.在现代 CMake 中,我发现最好在目标级别而不是全局变量级别分配 CXX 标准并使用内置属性(参见此处: https : //cmake.org/cmake/help/latest/manual/cmake -properties.7.html ) 以使其与编译器无关。
For Example:例如:
set_target_properties(FooTarget PROPERTIES
CXX_STANDARD 17
CXX_EXTENSIONS OFF
etc..
)
You can keep that set(CMAKE_CXX_STANDARD 17)
for other compilers, like Clang and GCC.您可以为其他编译器(如 Clang 和 GCC set(CMAKE_CXX_STANDARD 17)
保留该set(CMAKE_CXX_STANDARD 17)
。 But for Visual Studio, it's useless.但是对于 Visual Studio 来说,它是没有用的。
If CMake still doesn't support this, you can do the following:如果 CMake 仍然不支持此功能,您可以执行以下操作:
if(MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++17")
endif(MSVC)
使用vs2019
set(CMAKE_CXX_STANDARD 17)
Based on my test, the following code will enable VS2019 to select /std:c++latest, otherwise for other platforms it will set the C++ version to C++17.根据我的测试,下面的代码会启用VS2019选择/std:c++latest,否则其他平台会设置C++版本为C++17。 I have tested this with emscripten, raspbian, and windows.我已经用 emscripten、raspbian 和 windows 对此进行了测试。
if(MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++latest")
else(MSVC)
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_17)
endif(MSVC)
Bash command line in CMake flags: CMake 标志中的 Bash 命令行:
-DCMAKE_CXX_STANDARD=17 \
-DCMAKE_CXX_STANDARD_REQUIRED=ON \
-DCMAKE_CXX_EXTENSIONS=OFF \
You can also use target_compile_options
to set /std:c++latest
flag for Visual Studio 2019您还可以使用target_compile_options
为 Visual Studio 2019 设置/std:c++latest
标志
if (MSVC_VERSION GREATER_EQUAL "1900")
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("/std:c++latest" _cpp_latest_flag_supported)
if (_cpp_latest_flag_supported)
target_compile_options(${TARGET_NAME} PRIVATE "/std:c++latest")
endif()
endif()
Replace ${TARGET_NAME}
with the actual target name.将${TARGET_NAME}
替换${TARGET_NAME}
实际目标名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.