How to Enable C++17 Compiling in Visual Studio

How to enable C++17 compiling in Visual Studio?

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

How to enable C++17 support in VSCode C++ Extension

This has become much easier now. Search for cppstandard in your vs code extension settings and choose the version of C++ you want the extension to use from the drop down.

Sample Image

In order to make sure your debugger is using the same version, make sure you have something like this for your tasks.json, where the important lines are the --std and the line after that defines the version.

{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"--std",
"c++17",
"-I",
"${fileDirname}",
"-g",
"${fileDirname}/*.cpp",
"-o",
"${workspaceFolder}/out/${fileBasenameNoExtension}.o"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
}
}
],
"version": "2.0.0"
}

Note that if you're copying the above tasks.json directly, you'll need to have a folder named out in your workspace root.

How to enable C++17 code generation in VS2019 CUDA project

Using CUDA 11, the nvcc compiler-driver is capable of supporting usage of certain C++17 language features. Currently, in VS2019, this doesn't appear to be the default behavior.

The following method should work to enable C++17 support when compiling a cuda project in VS2019:

go to Project..Properties..Configuration Properties...CUDA C/C++...Command Line Then you will see a box in the right hand side bottom of the dialog labelled "Additional Options". In that box, type the following:

-std=c++17 -Xcompiler "/std:c++17"

then click "Apply" then rebuild.
Project Properties

(These instructions may change for future versions of CUDA or future versions of Visual Studio.)

Note that this method applies to CUDA projects only (i.e. when nvcc is invoked for compilation), and should be workable whether your code is in a file ending in .cpp or in a file ending in .cu. For non-CUDA projects, this may be helpful.

The std::filesystem features appear to require C++17. The CUDA nvcc compiler-driver is documented here.

How to enable c++17 on vscode(MAC)

Using two consecutive > to close out nested template declarations is not legal in C++98/03, but is legal in C++11 (and later).

Your command g++ filename.cpp is (presumably) defaulting to C++98, while when you try g++ -std=c++17 -g filename.cpp you are expressly indicating that you want to compile for C++17.

How do I upgrade to C++17?

You don't "upgrade" to newer C++ standards.

You can upgrade compiler to newer version supporting latest standards.

As of today, most compilers are set to C++14 by default.

To change it you need to pass additional argument during compilation.

For example, to compile hello.cpp with GCC for C++17 you need to execute

g++ -std=c++17 hello.cpp

You need to check how to pass compiler flags (or set standards) in your IDE / editor / build system.


I'm not familiar with Atom, but I've found this:

In the settings, click on Packages, then search for gpp-compiler. You should see a settings button – click on it and edit the command line options to suit your needs.

Visual Studio Code g++ does not compile with c++17 when debugging

It looks like there is "built-in" task with name "C/C++: g++ build active file", so that even if it's not defined in tasks.json but used in launch.json, it is launched anyway... with some default configuration you can't change, of course. Try to remove this task from tasks.json to find out that's true.

This built-in task also seems to have higher priority than the one defined by you. This is probably a bug in VSC, but simple workaround with changing task name seems to be doing the job.

Just rename your task to something else that default name. Ie your tasks.json would look like this (notice how label key is defined):

{
"tasks": [
{
"type": "cppbuild",
"label": "Totally custom debug task name",
"command": "/usr/bin/g++",
"args": [
"-std=c++17",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Generated task by Debugger"
}
],
"version": "2.0.0"
}

And your launch.json - like this (notice how preLaunchTask is defined):

{
"version": "0.2.0",
"configurations": [
{
"name": "g++ - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "Totally custom debug task name",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}

How to Setup VS Code For C++ 14 /C ++17

I added code runner and added this in settings.json Seems to work for me :D

"code-runner.executorMap": {
"cpp": "cd $dir && g++ -std=c++17 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
},


Related Topics



Leave a reply



Submit