如何在 Visual Studio 中启用 C++17 编译?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41308933/
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 compiling in Visual Studio?
提问by Tudvari
I want to use C++17 features.
我想使用 C++17 特性。
How can I switch compiling from C++14 to C++17 in Microsoft Visual Studio?
如何在 Microsoft Visual Studio 中将编译从 C++14 切换到 C++17?
Or it's not avaiable in release versions of VS?
或者它在 VS 的发布版本中不可用?
回答by Johan Lundberg
There's now a drop down (at least since VS 2017.3.5) where you can specifically select C++17. The available options are (under project > Properties > C/C++ > Language > C++ Language Standard)
现在有一个下拉菜单(至少从 VS 2017.3.5 开始),您可以在其中专门选择 C++17。可用选项是(在项目 > 属性 > C/C++ > 语言 > C++ 语言标准下)
- ISO C++14 Standard. msvc command line option:
/std:c++14
- ISO C++17 Standard. msvc command line option:
/std:c++17
- The latest draft standard. msvc command line option:
/std:c++latest
- ISO C++14 标准。msvc 命令行选项:
/std:c++14
- ISO C++17 标准。msvc 命令行选项:
/std:c++17
- 最新的标准草案。msvc 命令行选项:
/std:c++latest
(I bet, once C++20 is out and more fully supported by Visual Studio it will be /std:c++20
)
(我敢打赌,一旦 C++20 出来并得到 Visual Studio 的更全面支持,它将是/std:c++20
)
回答by Zingam
MSBuild (Visual Studio project/solution *.vcproj/*.sln):
MSBuild(Visual Studio 项目/解决方案*.vcproj/*.sln):
Add to Additional options in Project Settings: /std:c++latest
to enable latest features - currently C++17 as of VS2017, VS2015 Update 3.
添加到项目设置中的附加选项:/std:c++latest
启用最新功能 - 从 VS2017、VS2015 Update 3 开始,当前为 C++17。
https://blogs.msdn.microsoft.com/vcblog/2016/06/07/standards-version-switches-in-the-compiler/
https://blogs.msdn.microsoft.com/vcblog/2016/06/07/standards-version-switches-in-the-compiler/
/permissive-
will disable non-standard C++ extensions and will enable standard conformance in VS2017.
/permissive-
将禁用非标准 C++ 扩展并启用 VS2017 中的标准一致性。
https://blogs.msdn.microsoft.com/vcblog/2016/11/16/permissive-switch/
https://blogs.msdn.microsoft.com/vcblog/2016/11/16/permissive-switch/
EDIT (Oct 2018):The latest VS2017 features are documented here:
编辑(2018 年 10 月):此处记录了最新的 VS2017 功能:
https://docs.microsoft.com/en-gb/cpp/build/reference/std-specify-language-standard-version
https://docs.microsoft.com/en-gb/cpp/build/reference/std-specify-language-standard-version
VS2017 supports: /std:[c++14|c++17|c++latest]
now. These flags can be set via the project's property pages:
VS2017 支持:/std:[c++14|c++17|c++latest]
现在。这些标志可以通过项目的属性页设置:
To set this compiler option in the Visual Studio development environment
- Open the project's Property Pages dialog box. For details, see Working with Project Properties.
- Select Configuration Properties, C/C++, Language.
- In C++ Language Standard, choose the language standard to support from the dropdown control, then choose OK or Apply to save your changes.
在 Visual Studio 开发环境中设置此编译器选项
- 打开项目的属性页对话框。有关详细信息,请参阅使用项目属性。
- 选择配置属性、C/C++、语言。
- 在 C++ 语言标准中,从下拉控件中选择要支持的语言标准,然后选择确定或应用以保存更改。
CMake:
制作:
Visual Studio 2017 (15.7+) supports CMake projects. CMake makes it possible to enable modern C++ features in various ways. The most basic option is to enable a modern C++ standard by setting a target's propertyin CMakeLists.txt:
Visual Studio 2017 (15.7+) 支持 CMake 项目。CMake 使以各种方式启用现代 C++ 功能成为可能。最基本的选项是通过在 CMakeLists.txt 中设置目标的属性来启用现代 C++ 标准:
add_library (${PROJECT_NAME})
set_property (TARGET ${PROJECT_NAME}
PROPERTY
# Enable C++17 standard compliance
CXX_STANDARD 17
)
In the case of an interface library:
在接口库的情况下:
add_library (${PROJECT_NAME} INTERFACE)
target_compile_features (${PROJECT_NAME}
INTERFACE
# Enable C++17 standard compliance
cxx_std_17
)
回答by Remy Lebeau
Visual Studio 2015 Update 3 does not support the C++17 feature you are looking for (emplace_back()
returning a reference).
Visual Studio 2015 Update 3 不支持您正在寻找的 C++17 功能(emplace_back()
返回引用)。
Support For C++11/14/17 Features (Modern C++)
C++11/14/17 Features In VS 2015 RTM
VS 2015 Update 2's STL is C++17-so-far Feature Complete
回答by WilliamClements
If bringing existing Visual Studio 2015 solution into Visual Studio 2017 and you want to build it with c++17 native compiler, you should first Retarget the solution/projects to v141 , THEN the dropdown will appear as described above ( Configuration Properties -> C/C++ -> Language -> Language Standard)
如果将现有的 Visual Studio 2015 解决方案引入 Visual Studio 2017 并且您想使用 c++17 本机编译器构建它,您应该首先将解决方案/项目重定向到 v141 ,然后下拉列表将如上文所述(配置属性 -> C /C++ -> 语言 -> 语言标准)
回答by sanitizedUser
Visual studio 2019 version:
Visual Studio 2019 版本:
The drop down menu was moved to:
下拉菜单移至:
- Right click on project
- Properties (or Alt + Enter)
- From the left menu select Configuration Properties
- General
- In the middle there is an option called "C++ Language Standard"
- Next to it is the drop down menu
- Here you can select Default, ISO C++ 14, 17 or latest
- 右键单击项目
- 属性(或 Alt + Enter)
- 从左侧菜单中选择配置属性
- 一般的
- 中间有一个选项叫做“C++语言标准”
- 旁边是下拉菜单
- 在这里您可以选择默认、ISO C++ 14、17 或最新版本