Amir Khan
Amir Khan

Reputation: 97

How can I change the C++ standard used by the Code Runner extension for VS Code?

I'm trying to use C++20 in VS Code with the Code Runner extension, which allows me to run code using ctrl+alt+n.

I have installed MinGW64 through the MSYS2 installer. My compiler version is 12.0.2.

When I type __cplusplus in my C++ file and hover my mouse over it, the hover info says "# define __cplusplus 202002L".

But the Code Runner extension is still not using C++20. Why? How can I make it use C++20?

Upvotes: 2

Views: 4656

Answers (2)

W.Bing
W.Bing

Reputation: 49

I'm using VSCode in MacOS and encountered the same issue. And it's resolved by add -std=c++20 in the Cxxflags in setting for the extension C/C++ Clang Command Adapter. enter image description here

Upvotes: -1

starball
starball

Reputation: 51943

You need to edit the code-runner.executorMap setting to add the C++ standard selection flag there. Perhaps something like this:

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

You're seeing "# define __cplusplus 202002L" in your hover info because that's controlled by the vscode-cpptools extension- a separate extension from the Code Runner extension, which reads your c_cpp_properties-related settings.

tasks.json is a VS Code feature, which I assume you're using to define a build task. I think there's usually not much reason to have both a build task in the tasks.json as well as usage of Code Runner, because Code Runner will handle building and running (but it's really only good for extremely simple use-cases).

As far as I know, the Code Runner extension doesn't care about your tasks.json tasks, or your vscode-cpptools settings.

Loosely related issue ticket: Update default settings of code-runner.executorMap to support c++11 or higher versions #397.

Upvotes: 7

Related Questions