undefined reference to...异常的解决

本来想测试一下内联函数的与普通函数的执行效率问题,可是由于使用Linux系统不久,居然在编译的过程中出现错误。

首先编写了一个头文件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"," shirdrn@hotmail.com","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 :  shirdrn@hotmail.com
phone : 13843140000
married : 0
Logo

更多推荐