undefined reference to...異常的解決
首先編寫了一個頭文件person.h,如下所示:
#ifndef PERSON_H_
#define PERSON_H_
#include <iostream>
using namespace std;
class Person{
long id;
char* name;
int age;
char gender;
char* school;
int tall;
int weight;
char* level;
char* yourCompany;
char* addr;
char* email;
char* phone;
bool married;
public:
Person(long id,char* name,int age,char gender,char* school,int tall,int weight,char* level,char* yourCompany,char* addr,char* email,char* phone,bool married);
void getPerson();
~Person();
};
#endif
然後,編寫了一個Person.cpp,代碼如下所示:
#include "person.h"
Person::Person(long id,char* name,int age,char gender,char* school,int tall,int weight,char* level,char* yourCompany,char* addr,char* email,char* phone,bool married){
this -> id = id;
this -> name = name;
this -> age = age;
this -> gender = gender;
this -> school = school;
this -> tall = tall;
this -> weight = weight;
this -> level = level;
this -> yourCompany = yourCompany;
this -> addr = addr;
this -> email = email;
this -> phone = phone;
this -> married = married;
}
void Person::getPerson(){
cout << "id : " << id << endl;
cout << "name : " << name << endl;
cout << "age : " << age << endl;
cout << "gender : " << gender << endl;
cout << "school : " << school << endl;
cout << "tall : " << tall << endl;
cout << "weight : " << weight << endl;
cout << "yourCompany : " << yourCompany << endl;
cout << "addr : " << addr << endl;
cout << "email : " << email << endl;
cout << "phone : " << phone << endl;
cout << "married : " << married << endl;
}
Person::~Person(){
}
最後,測試的主函數如下所示:
#include "person.h"
int main(){
Person p(19830119,"shirdrn",26,'M',"CUST",185,136,"Master","SHIRDRN STUDIO","Changchun","[email protected]","13843140000",false);
p.getPerson();
return 0;
}
編譯的時候,我開始使用命令:
g++ -o Main Main.cpp
但是總是出錯,信息如下所示:
/tmp/ccxZZzvU.o: In function `main':
Main.cpp:(.text+0x1f2): undefined reference to `Person::Person(long, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, char, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool)'
Main.cpp:(.text+0x3a5): undefined reference to `Person::getPerson()'
Main.cpp:(.text+0x3d6): undefined reference to `Person::~Person()'
Main.cpp:(.text+0x3ec): undefined reference to `Person::~Person()'
collect2: ld returned 1 exit status
其實錯誤很明顯了,說明Person.cpp沒有編譯,導致連接失敗,所以提示“undefined reference to...”。
在網上找了半天,終於在一個很小的角落裏找到了一點點幫助,其實編譯的過程中需要將連接用到的文件全部編譯的。使用Eclipse的時候,建立一個Managed Make C++ Project,已經將所有的C++文件編譯過了,纔不會報錯的。
至於編寫Makefile還需要進一步學習的。
使用如下的編譯命令:
[root@host c++]# g++ -g -o Main Main.cpp Person.cpp
就可以成功編譯。然後運行:
[root@host c++]# ./Main
輸出結果:
id : 19830119
name : shirdrn
age : 26
gender : M
school : CUST
tall : 185
weight : 136
yourCompany : SHIRDRN STUDIO
addr : Changchun
email : [email protected]
phone : 13843140000
married : 0