我正在嘗試使用多個 .cpp 和 .hpp 檔案編譯一個 c 示例,但 g 沒有找到任何成員函式定義。
主.cpp:
#include <iostream>
#include "Person.hpp"
int main()
{
std::cout << "HELL!\n";
Person a{"Jiraya"};
std::cout << a.getName() << "\n";
a.setName("Niko");
a.do_smt();
}
人.hpp:
#pragma once
#include <string>
using std::string;
class Person
{
private:
string name;
public:
Person();
Person(const string &n);
void do_smt();
string getName(){return name;}
void setName(const string& n);
個人.cpp:
#pragma once
#include <iostream>
#include "Person.hpp"
Person::Person(const string &n) : name{n}
{
}
void Person::setName(const string &n)
{
name = n;
}
void Person::do_smt()
{
std::cout << "???";
}
任務.json:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C : g build active file",
"command": "/usr/bin/g ",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-iquote${workspaceFolder}/headers"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
終端輸出:
> Executing task: C/C : g build active file <
Starting build...
/usr/bin/g -fdiagnostics-color=always -g /home/raijin/Documents/Code/C /sandbox/main.cpp -o /home/raijin/Documents/Code/C /sandbox/main -iquote/home/raijin/Documents/Code/C /sandbox/headers
/usr/bin/ld: /tmp/ccN0pJKE.o: in function `main':
/home/raijin/Documents/Code/C /sandbox/main.cpp:9: undefined reference to `Person::Person(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/bin/ld: /home/raijin/Documents/Code/C /sandbox/main.cpp:11: undefined reference to `Person::setName(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/bin/ld: /home/raijin/Documents/Code/C /sandbox/main.cpp:12: undefined reference to `Person::do_smt()'
collect2: error: ld returned 1 exit status
Build finished with error(s).
Terminal will be reused by tasks, press any key to close it.
這是專案檔案夾結構:

我確實添加"-iquote${workspaceFolder}/headers"了 argtasks.json以在子目錄中找到 .hpp 。它似乎不適用于 .cpp 檔案。我該怎么辦?(即使我移動Person.cpp到${workspaceFolder}結果相同的終端輸出)
uj5u.com熱心網友回復:
在您tasks.json使用默認值時${file},這意味著只編譯活動檔案,而不是檔案夾結構中的所有源檔案。
VSCode 檔案解釋了如何在同一檔案夾中的所有源檔案的情況下解決此問題:https ://code.visualstudio.com/docs/cpp/config-linux#_modifying-tasksjson
解決方法是替換${file}為"${workspaceFolder}/*.cpp"
在您的情況下,您有超過 1 個包含源檔案的檔案夾。您可以通過添加以下內容對第二個檔案夾應用類似的修復:"${workspaceFolder}/classes/*.cpp"
所以整個tasks.json將是:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C : g build active file",
"command": "/usr/bin/g ",
"args": [
"-fdiagnostics-color=always",
"-g",
"${workspaceFolder}/*.cpp",
"${workspaceFolder}/classes/*.cpp",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-iquote${workspaceFolder}/headers"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/421455.html
標籤:
上一篇:Hexo博客部署至服務器
下一篇:如何在IAM委托人cloudformation模板映射中同時允許顯式角色ARN和通配符角色(甚至賬戶中的所有角色)?
