I am trying to compile C++ code with Geany.
Compile command: g++ -Wall -c "%f"
Build command: g++ -Wall -o "%e" "%f"
main.cpp:
#include <iostream>
#include "Person.hpp"
int main()
{
Person p1(16);
std::cout << p1.getAge();
return 0;
}
Person.hpp
class Person
{
public:
Person(int a);
void setAge(int);
int getAge() const;
private:
int age;
};
inline int Person::getAge() const
{
return age;
}
Person.cpp
#include "Person.hpp"
Person::Person(int a)
{
age = a;
}
void Person::setAge(int a)
{
age = a;
}
Error:
g++ -Wall -o "main" "main.cpp" (in directory: /home/me/projects/Test) /tmp/ccxYmWkE.o: In function
main': main.cpp:(.text+0x15): undefined reference to
Person::Person(int)' collect2: error: ld returned 1 exit status Compilation failed.
Before Geany, I only used Code::Blocks and everything worked fine. How can I fix it?
It's obvious you didn't add Person.cpp
to the compilation command. then it can not pass the linkage level.
Add -o Person Person.cpp
to the build option after g++ -Wall -c "%e" "%f"
.
After all the compile command should be something like below:
g++ -Wall -o "main" "main.cpp" -o Person Person.cpp