'Vscode g++ it isn't finding .cpp definition files
I'm trying to compile a c++ example with multiple .cpp and .hpp files, but g++ doesn't find any member function definition.
main.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();
}
Person.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);
Person.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 << "???";
}
Tasks.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"
}
Terminal output:
> 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.
Here is the project folder structure:
I did add "-iquote${workspaceFolder}/headers" arg in tasks.json to locate .hpp in a subdirectory. It doesn't seem to work with .cpp files. What do I do?(Even if I move Person.cpp to ${workspaceFolder} results the same terminal output)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

