“未定义的参考"在 G++ 中 [英] "undefined reference to" in G++ Cpp

查看:39
本文介绍了“未定义的参考"在 G++ 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎无法消除错误.错误如下.我在谷歌上看过,仍然无法弄清楚.好像我不是 Cpp 的新手,但有一段时间没有被它愚弄过.

Can't seem to get the errors to go away. Errors are below. I have looked on google and still can't figure it out. It is not like I am new to Cpp, but have not fooled with it in a while.

奇怪的是它可以在 Windows 中使用 G++...

错误:

  • [ze@fed0r!---**__*]$ g++ main.cpp
  • /tmp/ccJL2ZHE.o:在函数'main'中:
  • main.cpp:(.text+0x11): 对 `Help::Help()' 的未定义引用
  • main.cpp:(.text+0x1d): 对 `Help::sayName()' 的未定义引用
  • main.cpp:(.text+0x2e): 对 `Help::~Help()' 的未定义引用
  • main.cpp:(.text+0x46): 对 `Help::~Help()' 的未定义引用
  • collect2: ld 返回 1 个退出状态
  • [ze@fed0r! ---**__*]$ g++ main.cpp
  • /tmp/ccJL2ZHE.o: In function `main':
  • main.cpp:(.text+0x11): undefined reference to `Help::Help()'
  • main.cpp:(.text+0x1d): undefined reference to `Help::sayName()'
  • main.cpp:(.text+0x2e): undefined reference to `Help::~Help()'
  • main.cpp:(.text+0x46): undefined reference to `Help::~Help()'
  • collect2: ld returned 1 exit status

main.cpp

#include <iostream>
#include "Help.h"

using namespace std;

int main () {

    Help h;
    h.sayName();

    // ***

    // ***

    // ***
    return 0;

}

帮助.h

#ifndef HELP_H
#define HELP_H

class Help {
    public:
        Help();
        ~Help();
        void sayName();
    protected:
    private:
};

#endif // HELP_H

帮助.cpp

#include <iostream>
#include "Help.h"

using namespace std;

Help::Help() { // Constructor
}

Help::~Help() { // Destructor
}

void Help::sayName() {
    cout << "            ***************" << endl;
    cout << "   ************************************" << endl;
    cout << "              ************" << endl;
    cout << "         *********************" << endl;
}

推荐答案

g++ main.cpp Help.cpp

g++ main.cpp Help.cpp

你必须告诉编译器你希望它编译的所有文件,而不仅仅是第一个.

You have to tell the compiler all the files that you want it to compile, not just the first one.

这篇关于“未定义的参考"在 G++ 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆