如何使用CMake在VS2017中启用/ std:c ++ 17 [英] How to enable /std:c++17 in VS2017 with CMake
问题描述
我正在尝试使用CMake将 / std:c ++ 17
编译器标志添加到VS2017中。到目前为止,我使用的是现代跨平台方式:
I'm trying to add the /std:c++17
compiler flag to VS2017 with CMake. I'm using the "modern" cross-platform way so far:
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF) # -std=c++11 instead of -std=gnu++11
set(MY_CXX_COMPILE_FEATURES cxx_generic_lambdas cxx_range_for cxx_strong_enums)
add_library(mylib INTERFACE)
target_compile_features(mylib INTERFACE ${MY_CXX_COMPILE_FEATURES})
这会增加<$ c VS2017中的$ c> / std:c ++ 14 (无论如何可能是默认值?)。
但是我在将其切换到C ++ 17时遇到了麻烦(即让它添加 / std:c ++ 17
)。如果我只是手动添加它,则会收到不太好的警告,因为同时存在两个标志:
This adds /std:c++14
in VS2017 (which might be the default anyway?).
However I'm having trouble switching this to C++17 (i.e. having it add /std:c++17
). If I just add it manually, I get the not-so-nice warning because both flags are present:
1> cl:命令行警告D9025:用'/ std:c ++ 17'
$ b覆盖'/ std:c ++ 14'
$ b
我已经尝试过 set(CMAKE_CXX_STANDARD 17)
,但是它没有任何作用,实际上CMake文档中提到 CMAKE_CXX_STANDARD
还是对VS没有影响。
I've tried set(CMAKE_CXX_STANDARD 17)
but it has no effect, in fact the CMake documentation mentions that CMAKE_CXX_STANDARD
has no effect on VS anyway.
在 target_compile_features $中添加C ++ 17功能c $ c>,似乎还没有(即使在CMake-3.9.0-rc5中),即使有,我也只使用
std :: optional
来自C ++ 17,并且没有 target_compile_features
标志,这些库功能如 std :: optional
。
As for adding a C++17 feature to target_compile_features
, it doesn't seem like there are any yet (even in CMake-3.9.0-rc5), and even if there were, I'm specifically only using std::optional
from C++17, and there's no target_compile_features
flags for library features like std::optional
.
所以我的问题是,用CMake做到这一点的最好(或最丑陋)的方法是什么?并以某种方式也适用于gcc和clang?我很高兴使用最新的CMake版本(3.8或3.9)。我更喜欢它是 nice,而不是手动遍历CXX_COMPILE_FLAGS并删除字符串 / std:c ++ 14或类似的hack。
So my question is, what's the best (or least ugly) way to do this with CMake? And in a way so it'll also work for gcc and clang? I'm happy to use a very recent CMake version (3.8 or 3.9). I prefer it to be "nice" and not manually looping through CXX_COMPILE_FLAGS and removing the string "/std:c++14" or some hack like that.
( Edit :它也可以是VS / std:c ++ latest
开关-两者皆有可能。两者都可以使用。)
(Edit: It can also be the VS/std:c++latest
switch - whichever is possible. Both work for the purpose.)
推荐答案
将我的评论变成答案
-
CMake团队正在针对VS2017(截至2017年7月,即将推出的CMake 3.10版)进行开发:
The CMake team is working on it for VS2017 (as for July 2017, for upcoming CMake version 3.10):
那些标记似乎是相当新的开关(与该问题的日期有关):
Those flags seem to be rather new switches (as related to the date of this question):
VS 2017 15.3预览版现在支持/ std :c ++ 17
VS 2017 15.3 preview now supports /std:c++17
因此,对于Visual Studio,您必须手动替换或附加编译器开关,直到CMake正式支持它为止
So for Visual Studio you have to "manually" replace or append the compiler switches until CMake officially does support it.
以下是我针对 std:c ++ latest
(已受支持的代码段)进行测试的代码段。在我的CMake 3.8.0版本中):
Here is a code snippet that I've tested for std:c++latest
(which is already supported e.g. in my CMake 3.8.0 version):
if (MSVC_VERSION GREATER_EQUAL "1900")
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("/std:c++latest" _cpp_latest_flag_supported)
if (_cpp_latest_flag_supported)
add_compile_options("/std:c++latest")
endif()
endif()
对于CLang和GNU,支持是从2017年开始合并到主要的源代码分支中,并且是CMake 3.8及更高版本的一部分:
For CLang and GNU the support was merged into the main source code branch begin of 2017 and is part of CMake version 3.8 and above:
这篇关于如何使用CMake在VS2017中启用/ std:c ++ 17的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!