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

Ubuntu VS Code中GCC编译器C++标准设置问题(无法切换至C++23)

问题:Ubuntu 22.04 LTS下VS Code中GCC始终锁定C++17标准

我在全新安装的Ubuntu 22.04 LTS上使用VS Code搭配GCC编译器,无论调整何种设置,语言标准始终停留在C++17。通过以下代码检测:

#include <iostream>

int main()
{
    if (__cplusplus == 201703L) std::cout << "C++17\n";
    return 0;
}

输出始终为:C++17

已执行的操作:

  • 在c_cpp_properties.json中设置"cppStandard": "c++23";
  • 在C/C配置设置中将C标准设为c++23;
  • 尝试添加编译器参数-std=c++23;
  • 重启VS Code、新建文件、重装扩展,均无效。

当前tasks.json片段:

{
"tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++ build active file",
            "command": "/usr/bin/g++",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build",
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

已安装扩展:

  • C/C++(Microsoft)
  • C/C++ Extension Pack(Microsoft)
  • C/C++ Themes(Microsoft)
  • CMake Tools(Microsoft)
  • Better C++ Syntax(Jeff Hykin)

解决方法

  1. 确认GCC版本
    Ubuntu 22.04默认自带GCC 11.3,该版本对C++23的支持为实验性,需使用-std=c++2b而非-std=c++23启用(GCC 12及以上版本才支持-std=c++23参数)。终端执行g++ --version查看当前版本。

  2. 修改tasks.json添加编译参数
    注意:c_cpp_properties.json仅用于IntelliSense语法提示,实际编译标准由tasks.json中的参数决定。当前你的tasks.json未添加C++标准参数,需修改args数组:

"args": [
    "-fdiagnostics-color=always",
    "-g",
    "-std=c++2b", // GCC 11对应参数,若为GCC12+则替换为-std=c++23
    "${file}",
    "-o",
    "${fileDirname}/${fileBasenameNoExtension}"
]
  1. 升级GCC获取完整C++23支持(可选)
    若需要完整的C++23特性支持,可安装GCC 12或更高版本:
sudo apt update
sudo apt install gcc-12 g++-12

安装后将tasks.json中的command改为/usr/bin/g++-12,同时使用-std=c++23参数。

  1. 终端直接编译验证
    若修改后仍有问题,可在终端直接编译测试,确认参数是否生效:
g++ -std=c++2b 你的文件名.cpp -o 输出文件名
./输出文件名

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 10:40:59