You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让VS Code的Code Runner扩展使用C++20标准?

解决VS Code中Code Runner扩展不使用C++20标准的问题

问题原因

你之前配置的c_cpp_properties.json、settings.json里的C标准是给VS Code自带的C/C智能提示(IntelliSense)用的,tasks.json是给VS Code原生任务系统用的,但Code Runner扩展有独立的编译命令配置,完全不会读取这些设置——这就是为什么智能提示能识别C++20,但运行代码时还是用默认标准的原因。

解决步骤

  1. 打开VS Code设置(快捷键Ctrl+,),搜索Code Runner: Executor Map,点击「编辑 in settings.json」
  2. 在打开的settings.json里找到code-runner.executorMap下的cpp字段,修改编译命令,把-std=c++20参数加到g++后面:
    "code-runner.executorMap": {
        // 其他语言配置...
        "cpp": "g++ -std=c++20 $fullFileName -o $fileNameWithoutExt && $fileNameWithoutExt"
    }
    
  3. 保存配置后,用Ctrl+Alt+N重新运行代码,此时Code Runner就会以C++20标准编译你的代码了。

验证方法

可以写一段C++20专属特性的代码测试,比如:

#include <iostream>
#include <span>

int main() {
    int arr[] = {1,2,3,4};
    std::span<int> s(arr);
    for (auto num : s) {
        std::cout << num << " ";
    }
    std::cout << "\n__cplusplus value: " << __cplusplus << std::endl;
    return 0;
}

运行后如果能正常输出数组内容和202002L,就说明配置生效了。

内容的提问来源于stack exchange,提问作者Amir Khan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 11:42:47