VSCode 中将头文件和头文件函数分离,编译主函数跳出 undefined reference to 的问题解决

VSCode 编写 C++ (.h,.cpp 文件分离)代码,编写完成后,编译遇到了编译错误 undefined reference to xxx。

开始还以为使用了 -std=c++20 而不能使用 #include "xxx.h" 方式头文件,但仔细一想虽然引入了 import,也不至于向前兼容都成了问题😊。

接着觉得可能是没有编译全部的 .cpp 文件,只编译了当前执行的 main.cpp文件,导致找不到对应的类。试着 #include "xxx.cpp",就解决了问题,但也不能这样改呀,是不。

这时问题已经明了了,在 tasks.json 文件里配置了单文件编译,问题就出在这里。修改tasks.json文件中args栏的 {file}` `为 {fileDirname}\*.cpp。

至此,问题解决。

配置多 tasks,并设置快捷键:

bash 复制代码
{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "GCC Compile Multi-Cpps",
            "command": "C:\\CplusEnv\\mingw64\\bin\\g++.exe",
            "args": [
                "-std=c++20",
                "-fdiagnostics-color=always",
                "-g",
                //"${file}", // 编译当前打开(编辑)的 .cpp 文件
                "${fileDirname}/*.cpp",   // 编译当前打开的文件所在目录下的所有.cpp 文件
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "compiler: C:\\CplusEnv\\mingw64\\bin\\g++.exe"
        },
        {
            "type": "cppbuild",
            "label": "GCC Compile Single-Cpp",
            "command": "C:\\CplusEnv\\mingw64\\bin\\g++.exe",
            "args": [
                "-std=c++20",
                "-fdiagnostics-color=always",
                "-g",
                "${file}", // 编译当前打开(编辑)的 .cpp 文件
                //"${fileDirname}/*.cpp",   // 编译当前打开的文件所在目录下的所有.cpp 文件
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                //"isDefault": true
            },
            "detail": "compiler: C:\\CplusEnv\\mingw64\\bin\\g++.exe"
        },
    ]
}

配置快捷键:

bash 复制代码
// Place your key bindings in this file to override the defaults
[
    {
        "key": "ctrl+1",                    // 配置快捷键
        "command": "workbench.action.tasks.runTask",    // runtask
        //"args": "GCC Compile Multi-Cpps"    //  task 的名称,对应定义的label,不定义更换,可以弹框选择对应的task
    },
    {
        "key": "ctrl+2",
        "command": "workbench.action.tasks.runTask",
        "args": "GCC Compile Single-Cpp"
    }
]

按 ctrl+1 后出现

按 ctrl+2 直接以单文件方式编译。

为什么只编译不运行呢?等待后续解决。

相关推荐
独隅8 小时前
Visual Studio 2026 详细安装教程和配置指南
ide·visual studio
He少年10 小时前
【AI 辅助案例分享】
人工智能·c#·编辑器·ai编程
Robot_Nav11 小时前
AI 编程助手 Skill 完全指南:VS Code · Trae CN · Claude Code
人工智能·vscode·skill·trae·claude code
vortex512 小时前
Kali Linux 安装与使用 Code-OSS / VSCodium :VSCode 轻量替代
linux·运维·编辑器
lifewange13 小时前
Pycharm和IDEA中安装Cursor的方法
ide·pycharm·intellij-idea
生而为虫15 小时前
在VScode中使用Claude Code agent并配置模型(仅mac电脑实际操作,windows电脑未实际操作如有问题可留言)
windows·vscode·macos
skilllite作者16 小时前
Zed 1.0 编辑器深度评测与实战指南
开发语言·人工智能·windows·python·编辑器·agi
读书札记20221 天前
visual studio 调试技巧总结
ide·visual studio
袋子(PJ)2 天前
2026年常见的配置文件格式——为什么程序员永远在折腾缩进和括号?—— 常见配置文件进化史(md文档很详细)
编辑器
hacker7072 天前
Visual Studio安装教程(C#开发版)
ide·c#·visual studio