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

VSCode配置MSVC编译C++2020标准失败,求有效配置示例

解决VSCode中C++20编译配置问题

所需扩展

  • 你已安装的 Microsoft C/C++ Extension 和 Visual Studio Build Tools 完全满足需求,确保Build Tools中勾选了「C++生成工具」组件

示例代码 main.cpp

#include <iostream>
#include <format>

int main() {
    // C++20特性:std::format
    std::string msg = std::format("C++20 测试:{}", "std::format 功能正常");
    std::cout << msg << std::endl;

    // C++20特性:constexpr lambda
    constexpr auto add = [](int a, int b) { return a + b; };
    static_assert(add(3, 5) == 8, "常量表达式加法错误");

    return 0;
}

配置文件示例:c_cpp_properties.json

(该文件用于IntelliSense代码提示,需放在项目根目录的.vscode文件夹下)

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": ["${workspaceFolder}/**"],
            "defines": ["_DEBUG", "UNICODE", "_UNICODE"],
            "compilerPath": "C:/Program Files/Microsoft Visual Studio/2022/BuildTools/VC/Tools/MSVC/14.38.33130/bin/Hostx64/x64/cl.exe",
            "cStandard": "c17",
            "cppStandard": "c++20",
            "intelliSenseMode": "windows-msvc-x64",
            "compilerArgs": ["/std:c++20"]
        }
    ],
    "version": 4
}

注意:替换compilerPath为你本地Visual Studio Build Tools对应的cl.exe路径,版本号可能因安装情况不同而变化

配置文件示例:tasks.json

(该文件控制实际编译行为,需放在项目根目录的.vscode文件夹下)

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: cl.exe 生成活动文件",
            "command": "C:/Program Files/Microsoft Visual Studio/2022/BuildTools/VC/Tools/MSVC/14.38.33130/bin/Hostx64/x64/cl.exe",
            "args": [
                "/Zi",
                "/EHsc",
                "/std:c++20",
                "/Fe:",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "${file}"
            ],
            "options": {"cwd": "${fileDirname}"},
            "problemMatcher": ["$msCompile"],
            "group": {"kind": "build", "isDefault": true},
            "detail": "编译器: C:/Program Files/Microsoft Visual Studio/2022/BuildTools/VC/Tools/MSVC/14.38.33130/bin/Hostx64/x64/cl.exe"
        }
    ]
}

关键:必须在args数组中添加/std:c++20,因为tasks.json的编译参数会覆盖properties.json的配置

关键注意事项

  • tasks.json是实际编译的执行配置,properties.json仅用于代码提示,所以编译C++20特性的核心是在tasks.json中指定/std:c++20
  • 推荐通过「Developer Command Prompt for VS 2022」启动VSCode,这样会自动配置Build Tools的环境变量,避免手动路径配置出错
  • 验证方式:按Ctrl+Shift+B运行构建任务,查看编译输出无错误后,运行生成的exe文件确认C++20特性正常工作

内容的提问来源于stack exchange,提问作者Gotham Exodus

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 15:22:35