How to enable /std:c++17 in VS2017 with CMake(如何使用 CMake 在 VS2017 中启用/std:c++17)
问题描述
我正在尝试使用 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})
这在 VS2017 中添加了 /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' 覆盖 '/std:c++14'
我试过 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 特性,似乎还没有(即使在 CMake-3.9.0-rc5 中),即使有,我专门只使用 C++17 中的 std::optional
,并且没有像 std::optional
这样的库功能的 target_compile_features
标志.
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).我更喜欢它不错",而不是手动循环遍历 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.
(编辑:它也可以是 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):
CMake:MSVC 标准版本切换
那些标志似乎是相当新的开关(与此问题的日期有关):
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:特性:添加对 C++ 17 语言标准的支持
这篇关于如何使用 CMake 在 VS2017 中启用/std:c++17的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 CMake 在 VS2017 中启用/std:c++17
基础教程推荐
- 使用从字符串中提取的参数调用函数 2022-01-01
- 从 std::cin 读取密码 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01