2013-09-29 53 views
0

我想与Geany编译C++代码。Geany - 未定义的参考

编译命令:克++ -Wall -c “%F”

生成命令:克++ -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; 
} 

错误:

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.

Geany之前,我只用代码::块和一切工作正常。我该如何解决它?

+0

可以(并且可能应该)配置* Geany *使用'Makefile'用于GNU [制造](http://www.gnu.org /软件/制造/);你的build命令显然是错误的,你需要链接几个目标文件... –

回答

1

很明显,您没有将Person.cpp添加到编译命令中。那么它不能通过联动水平。

-o Person Person.cpp添加到g++ -Wall -c "%e" "%f"之后的构建选项。 毕竟编译命令应该是象下面这样:

g++ -Wall -o "main" "main.cpp" -o Person Person.cpp