Search code examples
c++visual-studio-codemingwmingw-w64vscode-code-runner

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.

  • In my c_cpp_properties.json file, I have set cppStandard to c++20.
  • For good measure, in my settings.json file, I added "C_Cpp.default.cppStandard": "c++20".
  • In my tasks.json file, I have added "-std=c++20" to the args field.

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?


Solution

  • 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.