Project scenario:
- Use vscode to compile C++ multi-file
Problem Description
When compiling and running, a warning pops up on the interface:
the terminal prompts an error:
正在启动生成...
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
生成已完成,但出现错误。
Cause Analysis:
- When using VSCode, when there are multiple files in the same folder, the compiler cannot find the referenced .h file definition
solution:
-
Open
.VScode\tasks.json
: -
Only 11 lines of code need to be changed
"${fileDirname}\\*.cpp",
-
The args parameter problem in the tasks.json file, because the parameter
${file}
represents the current file being compiled, and the multi-document contains multiple documents, the compiler cannot find the related files referenced by the current file, so change the parameter to${fileDirname}\\*.cpp
represent the current file that can be compiled All .cpp files in the directory. If you are compiling a .c file, change the parameter to${fileDirname}\\*.c
-
Reference article: Zhihu link