"undefined reference to" in g ++ Cpp

It seems that the errors have not disappeared. Errors below. I looked at Google and still can not understand. It doesn't look like I'm new to Cpp, but haven't cheated on it for a while.

It is strange that he worked with g ++ on Windows ...

Errors:

  • [see @ fed0r! --- ** __ *] $ g ++ main.cpp
  • /tmp/ccJL2ZHE.o: In the `main 'function:
  • main.cpp :(. text + 0x11): undefined link to `Help :: Help () '
  • main.cpp :(. text + 0x1d): undefined link to `Help :: sayName () '
  • main.cpp :(. text + 0x2e): undefined link to `Help :: ~ Help () '
  • main.cpp :(. text + 0x46): undefined link 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; } 

Help.h

 #ifndef HELP_H #define HELP_H class Help { public: Help(); ~Help(); void sayName(); protected: private: }; #endif // HELP_H 

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; } 
+7
source share
2 answers

g ++ main.cpp Help.cpp

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

+15
source

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

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

By dividing it into two lines, you can save compilation time (in case of large projects), because you can compile help.cpp only after changing it. 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 
+8
source

All Articles