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

Ubuntu系统VS Code编译C++程序出现链接错误求助

解决Ubuntu下VS Code编译C++出现标准库未定义引用的问题

问题原因

你使用C语言编译器gcc编译C代码,而gcc默认不会自动链接C标准库(libstdc++)。代码中用到了std::cout、operator new/delete等C++标准库特性,导致链接阶段出现大量未定义引用错误。

解决方案

1. 终端手动编译

直接使用C编译器g++代替gcc,它会自动链接C标准库:

g++ -fdiagnostics-color=always -g /home/ashu/Documents/Box/qs.cpp -o /home/ashu/Documents/Box/qs

如果坚持用gcc,需要手动添加链接标准库的参数:

gcc -fdiagnostics-color=always -g /home/ashu/Documents/Box/qs.cpp -o /home/ashu/Documents/Box/qs -lstdc++

2. 修改VS Code的编译任务配置

打开VS Code的编译任务配置文件(tasks.json),调整编译命令:

  • 找到任务中的command字段,将"gcc"改为"g++",修改后的任务片段示例:
{
    "type": "cppbuild",
    "label": "C/C++: g++ build active file",
    "command": "/usr/bin/g++",
    "args": [
        "-fdiagnostics-color=always",
        "-g",
        "${file}",
        "-o",
        "${fileDirname}/${fileBasenameNoExtension}"
    ],
    // 其他配置项...
}
  • 或者保持gcc不变,在args数组末尾添加"-lstdc++"参数:
"args": [
    "-fdiagnostics-color=always",
    "-g",
    "${file}",
    "-o",
    "${fileDirname}/${fileBasenameNoExtension}",
    "-lstdc++"
]

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 18:12:01