7

I can't seem to get the errors to go away. The errors are below. I have looked on Google Search and still I can't figure it out. It is not like I am new to C++, but I have not fooled around with it in a while.

The weird thing is it worked with g++ on Windows...

Errors using:

g++ main.cpp

Output:

/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

File main.cpp

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

using namespace std;

int main () {

    Help h;
    h.sayName();

    // ***

    // ***

    // ***
    return 0;

}

File Help.h

#ifndef HELP_H
#define HELP_H

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

#endif // HELP_H

File Help.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;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Zeveso
  • 1,274
  • 3
  • 21
  • 41
  • Re *"The weird thing is it worked with g++ on Windows..."*: What platform was this tried on? Did it work in the past on Windows, but now it doesn't work on Windows? Or was this tried on something else than Windows, like Linux? The shell prompt suggests [Git Bash](https://superuser.com/questions/1053633/what-is-git-bash-for-windows-anyway) or [Cygwin](https://en.wikipedia.org/wiki/Cygwin) on Windows or perhaps Linux. – Peter Mortensen May 11 '22 at 12:10
  • OK, the OP has left the building: *"Last seen more than 8 years ago"*. We may never know. [Another question](https://stackoverflow.com/questions/5689437/compile-cpp-file-to-exe-inside-of-programexe) 4 months prior suggests the platform of choice could be Windows. – Peter Mortensen May 11 '22 at 12:15

3 Answers3

15

Use

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
john
  • 85,011
  • 4
  • 57
  • 81
8

You should add help.o to your g++ line:

g++ -c help.cpp -o help.o
g++ help.o main.cpp

By splitting it to two lines you can save compilation time (in case of larger projects), because you can compile help.cpp only when it was changed. make and Makefile used well will save you a lot of headache:

#Makefile
all: main

main: help main.cpp
    g++ -o main help.o main.cpp

help: help.cpp
    g++ -c -o help.o help.cpp
Jakub M.
  • 32,471
  • 48
  • 110
  • 179
0

I had the same problem with my Linux Lubuntu distribution and it was creating the problem for my constructor and destructor. It was not recognizing them.

Actually, this goes off if you just compile all of the three files together. So, once you saved all your files, just do this:

g++ main.cpp Help.h Help.cpp
./a.out

./a.out is the executable file for the Linux. Sorry, but I don't know about the Windows. And your program would run smoothly.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131