一行代码解决vscode编译C++多文件时 undefined reference to “XXX” 问题

本文解决VSCode中编译C++多文件时遇到的未定义引用错误,通过修改tasks.json文件中的参数来确保编译器能正确链接所有相关文件。

项目场景:

  • 使用vscode编译C++多文件

问题描述

编译运行时界面弹出警告:
错误警报
终端提示出现错误:

正在启动生成...
E:\x86_64-8.1.0-release-win32-seh-rt_v6-rev0\mingw64\bin\g++.exe -fdiagnostics-color=always -g G:\Code\C++hexinbiancheng\test_20\1.cpp -o G:\Code\C++hexinbiancheng\test_20\coin\1.exe -fexec-charset=GBK
c:\user\default\AppData\Local\Temp\ccJ7D1Gm.o: In function `isInCirle(Circle&, Point&)':
G:/Code/C++hexinbiancheng/test_20/1.cpp:74: undefined reference to `Circle::getCenter()'
G:/Code/C++hexinbiancheng/test_20/1.cpp:74: undefined reference to `Point::getX()'
G:/Code/C++hexinbiancheng/test_20/1.cpp:74: undefined reference to `Point::getX()'
G:/Code/C++hexinbiancheng/test_20/1.cpp:74: undefined reference to `Circle::getCenter()'
G:/Code/C++hexinbiancheng/test_20/1.cpp:74: undefined reference to `Point::getX()'
G:/Code/C++hexinbiancheng/test_20/1.cpp:74: undefined reference to `Point::getX()'
G:/Code/C++hexinbiancheng/test_20/1.cpp:74: undefined reference to `Circle::getCenter()'
G:/Code/C++hexinbiancheng/test_20/1.cpp:74: undefined reference to `Point::getY()'
G:/Code/C++hexinbiancheng/test_20/1.cpp:74: undefined reference to `Point::getY()'
G:/Code/C++hexinbiancheng/test_20/1.cpp:74: undefined reference to `Circle::getCenter()'
G:/Code/C++hexinbiancheng/test_20/1.cpp:74: undefined reference to `Point::getY()'
G:/Code/C++hexinbiancheng/test_20/1.cpp:74: undefined reference to `Point::getY()'
G:/Code/C++hexinbiancheng/test_20/1.cpp:77: undefined reference to `Circle::getR()'
G:/Code/C++hexinbiancheng/test_20/1.cpp:77: undefined reference to `Circle::getR()'
c:\user\default\AppData\Local\Temp\ccJ7D1Gm.o: In function `main':
G:/Code/C++hexinbiancheng/test_20/1.cpp:101: undefined reference to `Point::setX(int)'
G:/Code/C++hexinbiancheng/test_20/1.cpp:102: undefined reference to `Point::setY(int)'
G:/Code/C++hexinbiancheng/test_20/1.cpp:106: undefined reference to `Circle::setR(int)'
G:/Code/C++hexinbiancheng/test_20/1.cpp:108: undefined reference to `Point::setX(int)'
G:/Code/C++hexinbiancheng/test_20/1.cpp:109: undefined reference to `Point::setY(int)'
G:/Code/C++hexinbiancheng/test_20/1.cpp:110: undefined reference to `Circle::setCenter(Point)'
collect2.exe: error: ld returned 1 exit status

生成已完成,但出现错误。

原因分析:

  • 使用VSCode时,在同一文件夹下,存在多个文件时,编译器无法找到引用的.h文件定义

解决方案:

  1. 打开.VScode\tasks.json在这里插入图片描述

  2. 仅需要更改11行一行代码 "${fileDirname}\\*.cpp",
    在这里插入图片描述

  3. tasks.json文件中的args参数问题,由于参数${file}代表的是被编译的当前文件,而多文档包含多个文档,编译器无法找到当前文件所引用的相关文件,因此将参数改为${fileDirname}\\*.cpp代表可以编译当前目录下的所有.cpp文件。如果是编译.c文件则将参数改为${fileDirname}\\*.c

  4. 参考文章:知乎链接

### 解决 VSCode 中 `undefined reference to 'xxx'` 编译错误 #### 配置多文件编译环境 当遇到 `undefined reference to 'xxx'` 错误,通常意味着链接器无法找到某些函数或变量的定义。这可能是由于未正确配置多个源文件之间的依赖关系所致。 对于 C++ 项目而言,确保 `.h` 和 `.cpp` 文件之间的一致性非常重要。具体来说,需确认头文件内的函数签名与实现部分完全匹配[^3]。此外,还需验证是否已将所有必要的源文件加入到构建过程中。 #### 修改 settings.json 设置 Code Runner 执行映射 为了使 VSCode 能够处理多文件项目的编译需求,可以通过调整 `settings.json` 来指定自定义命令用于执行代码。例如: ```json { "code-runner.executorMap": { "cpp": "g++ $fullFileName -o $dir$fileNameWithoutExt && $dir$fileNameWithoutExt" } } ``` 上述 JSON 片段展示了如何通过修改 executor map 让 g++ 同编译并链接当前工作目录下的所有 cpp 文件[^5]。 #### 添加缺失的 main 函数入口点 有该类错误也可能源于缺少程序启动所需的主函数(main function),特别是在 Windows 平台上可能会显示为 `undefined reference to WinMain@16` 的形式。此应检查是否有适当定义了 main 或者其他平台特定的入口函数。 #### 完整示例:创建简单的 C++ 工程结构 下面是一个完整的例子来展示如何组织一个多文件 C++ 应用程序,并确保其可以在 VSCode 下顺利编译运行而不会触发类似的链接失败问题: 假设有一个名为 `fun.h` 的头文件以及对应的实现文件 `fun.cpp`, 还有包含应用程序逻辑起点的 `main.cpp`. - **fun.h** ```c++ #ifndef FUN_H_ #define FUN_H_ void fun(); #endif /*FUN_H_*/ ``` - **fun.cpp** ```c++ #include <iostream> #include "fun.h" void fun() { std::cout << "Function called from another file." << std::endl; } ``` - **main.cpp** ```c++ #include <iostream> #include "fun.h" int main(int argc, char* argv[]) { std::cout << "Hello world!" << std::endl; fun(); return 0; } ``` 最后一步是在终端里输入如下指令来进行手动编译测试(也可以利用之前提到的方法让 code runner 自动完成): ```bash g++ main.cpp fun.cpp -o myapp && ./myapp ``` 这样就可以成功避免出现 `undefined reference` 类型的问题了[^4].
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不会是要长脑子了吧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值