如何更改Code Runner扩展在VS Code中使用的C++标准?

huangapple go评论66阅读模式
英文:

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?

英文:

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

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 &quot;C_Cpp.default.cppStandard&quot;: &quot;c++20&quot;.
  • In my tasks.json file, I have added &quot;-std=c++20&quot; 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?

答案1

得分: 2

"你需要编辑code-runner.executorMap设置,以在那里添加C++标准选择标志。可能类似于这样:

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

你在悬停信息中看到"# define __cplusplus 202002L",是因为它由vscode-cpptools扩展控制,这是Code Runner扩展的一个单独扩展,它会读取你的与c_cpp_properties 相关的设置。

tasks.json 是 VS Code 的功能,我假设你正在使用它来定义构建任务。我认为通常没有太多理由同时在 tasks.json 中定义构建任务并使用 Code Runner,因为 Code Runner 将处理构建和运行(但它仅适用于非常简单的用例)。

据我所知,Code Runner 扩展不关心你的 tasks.json 任务或你的 vscode-cpptools 设置。

与之有关的问题票据:更新code-runner.executorMap的默认设置以支持c++11或更高版本#397."

英文:

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

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

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.

huangapple
  • 本文由 发表于 2023年3月31日 03:47:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75892388.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定