Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"undefined reference to" in G++ Cpp

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;
}
like image 762
Zeveso Avatar asked Aug 08 '11 05:08

Zeveso


People also ask

How do you fix a undefined reference in C++?

You can fix undefined reference in C++ by investigating the linker error messages and then providing the missing definition for the given symbols. Note that not all linker errors are undefined references, and the same programmer error does not cause all undefined reference errors.

How do I fix linker error in C++?

You can fix the errors by including the source code file that contains the definitions as part of the compilation. Alternatively, you can pass . obj files or . lib files that contain the definitions to the linker.

How do you fix undefined reference to function in C?

c file. The error: undefined reference to function show() has appeared on the terminal shell as predicted. To solve this error, simply open the file and make the name of a function the same in its function definition and function call. So, we used to show(), i.e., small case names to go further.

What is G ++ compiler?

g++ command is a GNU c++ compiler invocation command, which is used for preprocessing, compilation, assembly and linking of source code to generate an executable file. The different “options” of g++ command allow us to stop this process at the intermediate stage.


2 Answers

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.

like image 200
john Avatar answered Oct 20 '22 21:10

john


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
like image 8
Jakub M. Avatar answered Oct 20 '22 22:10

Jakub M.