如何在 CMake 中启用 C++17
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45688522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to enable C++17 in CMake
提问by MiP
I'm using VS 15.3, which supports integrated CMake 3.8. How can I target C++17 without writing flags for each specific compilers? My current global settings don't work:
我正在使用 VS 15.3,它支持集成的 CMake 3.8。如何在不为每个特定编译器编写标志的情况下以 C++17 为目标?我当前的全局设置不起作用:
# 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++lastest")
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.
采纳答案by Some programmer dude
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.9documentation:
来自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++. See the 3.10 documentation.
在 CMake 3.10(及更高版本)中,这已针对较新版本的 VC++ 修复。请参阅3.10 文档。
回答by nbout
Modern CMake propose an interface for this purpose target_compile_features
.
Documentation is here: Requiring Language Standards
现代 CMake 为此提出了一个接口target_compile_features
。文档在这里:需要语言标准
Use it like this:
像这样使用它:
target_compile_features(${TARGET_NAME} PRIVATE cxx_std_17)
target_compile_features(${TARGET_NAME} PRIVATE cxx_std_17)
回答by loneraver
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..
)
回答by The Quantum Physicist
You can keep that set(CMAKE_CXX_STANDARD 17)
for other compilers, like Clang and GCC. But for Visual Studio, it's useless.
您可以set(CMAKE_CXX_STANDARD 17)
为其他编译器保留它,例如 Clang 和 GCC。但是对于 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)