英文:
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
toc++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 theargs
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
toc++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 theargs
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++标准选择标志。可能类似于这样:
"code-runner.executorMap": {
"cpp": "cd $dir && g++ -std=c++20 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt"
}
你在悬停信息中看到"# 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:
"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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论