链接器错误 - 未定义的引用

Posted

技术标签:

【中文标题】链接器错误 - 未定义的引用【英文标题】:Linker error - undefined reference 【发布时间】:2015-12-01 03:26:39 【问题描述】:

我正在创建一个程序,但是当我运行它时出现以下错误:

/tmp/ccYLF4IM.o: In function `main':
maingame.cpp:(.text+0x13): undefined reference to `Human::Human()'
maingame.cpp:(.text+0x1f): undefined reference to `Orc::Orc()'
maingame.cpp:(.text+0x99): undefined reference to `Orc::~Orc()'
maingame.cpp:(.text+0xac): undefined reference to `Orc::~Orc()'
maingame.cpp:(.text+0xc5): undefined reference to `Human::~Human()'
maingame.cpp:(.text+0xdf): undefined reference to `Human::~Human()'
collect2: ld returned 1 exit status

我查看了我的代码,我正在同时运行所有 .cpp 文件和 .h 文件。我不确定问题可能是什么。我知道对此还有其他问题,但我似乎无法找到我正在寻找的答案。我检查了所有内容的大小写,我什至删除了所有内容并重新开始以确保。对此的任何帮助将不胜感激。谢谢你

//main.cpp
#include "Human.h"
#include "Orc.h"

using namespace std;

//Main.cpp
int main()

    //Character cc;
    Human H;
    Orc O;
    char choice;
    char userC;
    cout << "Welcome!\n";
    cout << "" << endl;

    cout << "Pick your choice:\n";
    cout << "A -- Human\n";
    cout << "B -- Orc\n";
    cin >> choice;

    return 0;


//Character.h
#ifndef CHARACTER_H
#define CHARACTER_H

#include <iostream>
#include <string>

using namespace std;

class Character

    protected:
                float characterTotal;
    public:                 
                virtual int createCharacter() = 0; //Pure virtual function
                Character();
                ~Character();   
;

#endif

//Character.cpp
#include "Character.h"
Character::Character()

//ctor


Character::~Character()

//dtor


//human.h
#ifndef HUMAN_H
#define HUMAN_H

#include "Character.h"

using namespace std;

class Human : public Character 

    private:
                int characterStrength;
                int characterDexterity;
                int characterIntelligence;
                string characterType;
                int characterTotal;

    public:
                Human();//Constructor 
                                ~Human();

                int getStrength ()
                
                    cout << "Enter a number from 0 to 18\n";
                    cin >> characterStrength;

                    return characterStrength;
                

                int getDexterity ()
                
                    cout << "Enter a number from 0 to 18\n";
                    cin >> characterDexterity;

                    return characterDexterity;
                

                int getIntelligence ()
                
                    cout << "Enter a number from 0 to 18\n";
                    cin >> characterIntelligence;

                    return characterIntelligence;
                

                string getType ()
                
                    cout << "Please choose one of the following\n";
                    cout << "A -- Paladin \n";
                    cout << "B -- Ranger \n";
                    cout << "C -- Wizard \n";\
                    cin >> characterType;

                    return characterType;
                

                virtual int createCharacter()
                
                    characterTotal = characterStrength + characterIntelligence + characterDexterity;
                    return characterTotal;
                
;

#endif

//Human.cpp
#include "Human.h"
Human::Human()

//ctor


Human::~Human()

//dtor


//orc.h
#ifndef ORC_H
#define ORC_H

#include "Character.h"

using namespace std;

class Orc : public Character

    private:
                int characterStrength;
                int characterDexterity;
                int characterIntelligence;
                int chaD;
                int chaI;
                int chaS;
                string characterClan;
                int characterTotal;

    public:
                Orc(); //Constructor
                        ~Orc();

                int getStrength()
                
                    cout << "Enter a number between 0 to 18\n";
                    cin >> chaS;
                    characterStrength = chaS + 2;

                    return characterStrength;
                

                int getDexterity()
                
                    cout << "Enter a number between 0 to 18\n";
                    cin >> chaD;
                    characterDexterity = chaD - 2;

                    return characterDexterity;
                

                int getIntelligence()
                
                    cout << "Enter a number between 0 to 18\n";
                    cin >> chaI;
                    characterIntelligence = chaI - 2;

                    return characterIntelligence;
                

                string getClan()
                
                    cout << "Please choose one of the following\n";
                    cout << "A -- Barbarian \n";
                    cout << "B -- Berserker \n";
                    cout << "C -- Vanguard \n";\
                    cin >> characterClan;

                    return characterClan;
                

                virtual int createCharacter()
                    
                        characterTotal = characterStrength + characterIntelligence + characterDexterity;
                        return characterTotal;
                    

;
#endif

//Orc.cpp
#include "Orc.h"
Orc::Orc()

//ctor


Orc::~Orc()

//dtor

【问题讨论】:

你是如何编译和链接这些文件的? 我的学校使用 Secure Shell,所以我就是这样编译它们的。它们都放在同一个文件夹中。 您打开 Secure Shell,它会自动为您编译和链接它们?有趣的设置... 【参考方案1】:

您需要编译所有您的源文件(不是头文件)并将它们链接在一起。此时,最直接的方法就是在一个命令中指定所有文件名:

g++ maingame.cpp character.cpp human.cpp orc.cpp

大型项目通常会单独编译每个文件,然后链接生成的.o(对象)文件,但您似乎还没有达到这一点。

【讨论】:

我刚刚运行了它。谢谢【参考方案2】:

immibis 有道理,但我认为您应该花一些时间研究一下 makefile。因为看起来你正在构建一个安静的项目,并且每次手动编译它都不舒服。 This 教程是对 makefile 的一个很好的介绍。永远不要停止学习!

【讨论】:

以上是关于链接器错误 - 未定义的引用的主要内容,如果未能解决你的问题,请参考以下文章

链接器错误 - 未定义的引用 -gcc

链接器错误:未定义对“QGLViewer 的 vtable”的引用

链接器错误:未定义对符号“glOrtho”的引用

链接器错误:未定义的函数引用

这些'未定义的对 __glut*WithExit' OpenGL 链接器错误的引用是啥意思? [复制]

链接器错误,使用 g++ 链接到由 gcc 编译的库,未定义对该函数的引用