如何在 Visual Studio 中启用 C++17 编译?

发布于 2025-01-18 09:51:32 字数 106 浏览 2 评论 0原文

我想使用 C++17 功能。

如何在 Microsoft Visual Studio 中将编译从 C++14 切换到 C++17?

或者说它在 VS 的发布版本中不可用?

I want to use C++17 features.

How can I switch compiling from C++14 to C++17 in Microsoft Visual Studio?

Or is it not available in release versions of VS?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

故事与诗 2025-01-25 09:51:32

现在有一个下拉菜(至少从vs 2017.3.5起),您可以在其中专门选择C ++ 17。可用的选项(在Project≫属性下> c/c ++语言> c ++语言标准)

  • ISO C ++ 14标准。 MSVC命令行选项:/std:C ++ 14
  • ISO C ++ 17标准。 MSVC命令行选项:/std:C ++ 17

Visual Studio 2022( MSVC C ++ 20和/std:C ++ 20开关-C ++团队博客):

  • ISO C ++ 20标准。 MSVC命令行选项:/std:C ++ 20

任何Visual Studio:

  • 最新草稿标准。 MSVC命令行选项:/std:C ++最新

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)

  • ISO C++14 Standard. msvc command line option: /std:c++14
  • ISO C++17 Standard. msvc command line option: /std:c++17

Visual Studio 2022 (MSVC C++20 and the /std:c++20 Switch - C++ Team Blog):

  • ISO C++20 Standard. msvc command line option: /std:c++20

Any Visual Studio:

  • The latest draft standard. msvc command line option: /std:c++latest
与酒说心事 2025-01-25 09:51:32

MSBuild(Visual Studio 项目/解决方案*.vcproj/*.sln

添加到项目设置中的其他选项:/std:c++latest 以启用最新功能 - 目前为 VS2017、VS2015 Update 3 的 C++17。

https://blogs.msdn。 microsoft.com/vcblog/2016/06/07/standards-version-switches-in-the-compiler/

/permissive- 将禁用非标准 C++ 扩展并将在 VS2017 中实现标准一致性。

https://blogs.msdn.microsoft.com/vcblog/ 2016/11/16/permissive-switch/

编辑(2018 年 10 月): 最新的 VS2017 功能记录如下:

https://learn.microsoft.com/en -gb/cpp/build/reference/std-specify-language-standard-version

VS2017 支持:/std:[c++14|c++17|c++latest] 现在。这些标志可以通过项目的属性页设置:

在Visual Studio开发中设置此编译器选项
环境

  1. 打开项目的属性页对话框。详细信息请参见工作
    与项目属性。
  2. 选择配置属性、C/C++、
    语言。
  3. 在 C++ 语言标准中,选择要执行的语言标准
    从下拉控件中选择支持,然后选择“确定”或“应用”保存
    您的更改。

CMake

Visual Studio 2017 (15.7+) 支持 CMake 项目。
CMake 可以通过多种方式启用现代 C++ 功能。最基本的选项是通过设置目标的属性 在 CMakeLists.txt 中:

add_library (${PROJECT_NAME})
set_property (TARGET ${PROJECT_NAME}
  PROPERTY
    # Enable C++17 standard compliance
    CXX_STANDARD 17
)

对于接口库:

add_library (${PROJECT_NAME} INTERFACE)
target_compile_features (${PROJECT_NAME}
  INTERFACE
    # Enable C++17 standard compliance
    cxx_std_17
)

MSBuild (Visual Studio project/solution *.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.

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.

https://blogs.msdn.microsoft.com/vcblog/2016/11/16/permissive-switch/

EDIT (Oct 2018): The latest VS2017 features are documented here:

https://learn.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:

To set this compiler option in the Visual Studio development
environment

  1. Open the project's Property Pages dialog box. For details, see Working
    with Project Properties.
  2. Select Configuration Properties, C/C++,
    Language.
  3. In C++ Language Standard, choose the language standard to
    support from the dropdown control, then choose OK or Apply to save
    your changes.

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 property in CMakeLists.txt:

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
)
別甾虛僞 2025-01-25 09:51:32

Visual studio 2019 版本:

下拉菜单已移至:

  • 右键单击项目(不是解决方案)
  • 属性(或 Alt + Enter)
  • 来自左侧菜单选择配置属性
  • 常规
  • 中间有一个名为“C++语言标准”的选项
  • 旁边是下拉菜单
  • 在这里您可以选择默认、ISO C++ 14、17或最新

Visual studio 2019 version:

The drop down menu was moved to:

  • Right click on project (not solution)
  • 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
放飞的风筝 2025-01-25 09:51:32

Visual Studio 2015 Update 3 不支持您正在寻找的 C++17 功能(emplace_back() 返回引用)。

支持 C++11/14/17 功能(现代 C++)

<一href="https://blogs.msdn.microsoft.com/vcblog/2015/06/19/c111417-features-in-vs-2015-rtm/" rel="noreferrer">C++11/14/17 VS 2015 RTM 中的功能

VS 2015 Update 2 的 STL 是 C++17 到目前为止功能完整

Visual Studio 2015 更新 3

VS 2015 Update 3 中的 STL 修复

氛圍 2025-01-25 09:51:32

如果将现有的 Visual Studio 2015 解决方案引入 Visual Studio 2017 并且您想使用 c++17 本机编译器构建它,您应该首先将解决方案/项目重新定位到 v141 ,然后下拉列表将按上述方式显示(配置属性 -> ) C/C++ -> 语言 -> 语言标准)

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)

一梦等七年七年为一梦 2025-01-25 09:51:32

vs代码2020版本

tasks.json file,(使用g ++ -9构建和调试后)

add -std = C ++ 2A 2020功能(C ++ 1Z 2017功能)。
添加-fconcepts以使用概念关键字

"args": [
   "-std=c++2a",
   "-fconcepts",
   "-g",
   "${file}",
   "-o",
   "${fileDirname}/${fileBasenameNoExtension}"
],

现在编译,您可以使用2020个功能。

VS Code 2020 version

In tasks.json file, (after you build and debug with the g++-9)

Add -std=c++2a for 2020 features (c++1z for 2017 features).
Add -fconcepts to use concept keyword

"args": [
   "-std=c++2a",
   "-fconcepts",
   "-g",
   "${file}",
   "-o",
   "${fileDirname}/${fileBasenameNoExtension}"
],

now compile and you can use the 2020 features.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文