vscode が C++ マルチファイルをコンパイルする際に発生する「XXX」への未定義の参照の問題は、1 行のコードで解決されます。

プロジェクトのシナリオ:

  • 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. task.json ファイルの args パラメーターの問題。このパラメーターは${file}コンパイル中の現在のファイルを表し、マルチドキュメントには複数のドキュメントが含まれているため、コンパイラーは現在のファイルによって参照される関連ファイルを見つけることができません。そのため、パラメーターを変更して${fileDirname}\\*.cpp、コンパイル可能な現在のファイル ディレクトリ内のすべての .cpp ファイル。.c ファイルをコンパイルしている場合は、パラメータを次のように変更します。${fileDirname}\\*.c

  4. 参考記事:志胡リンク

おすすめ

転載: blog.csdn.net/TianHW103/article/details/127320611