“未定义的参考"在 G++ 中

2022-01-23 00:00:00 g++ c++ undefined-reference

似乎无法消除错误.错误如下.我在谷歌上看过,仍然无法弄清楚.好像我不是 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.

相关文章