Linking files in g++

2019-01-15 02:00发布

问题:

Recently I have tried to compile a program in g++ (on Ubuntu). Usually i use Dev-C++ (on Windows) and it works fine there as long as I make a project and put all the necessary files in there.

The error that occurs when compiling the program is:

$filename.cpp: undefined reference to '[Class]::[Class Member Function]'

The files used are as following:

The source code (.cpp) file with the main function.

The header file with the function prototypes.

The .cpp file with the definitions for each function.

Any help will be appreciated.

回答1:

You probably tried to either compile and link instead of just compiling source files or somehow forgot something.

Variation one (everything in one line; recompiles everything all the time):

g++ -o myexecutable first.cpp second.cpp third.cpp [other dependencies, e.g. -Lboost, -LGL, -LSDL, etc.]

Variation two (step by step; if no -o is provided, gcc will reuse the input file name and just change the extension when not linking; this variation is best used for makefiles; allows you to skip unchanged parts):

g++ -c first.cpp
g++ -c second.cpp
g++ -c third.cpp
g++ -o myexecutable first.o second.o third.o [other dependencies]

Variation three (some placeholders):

Won't list it but the parameters mentioned above might as well take placeholders, e.g. g++ -c *.cpp will compile all cpp files in current directory to o(bject) files of the same name.

Overall you shouldn't worry too much about it unless you really have to work without any IDE. If you're not that proficient with the command line syntax, stick to IDEs first.



回答2:

The command line of gcc should look like:

g++ -o myprogram class1.cpp class2.cpp class3.cpp main.cpp

Check in which cpp file the missing class member function is defined. You may have not given it to gcc.



回答3:

You can also check for correct #include tags within filename.cpp. Assume that filename.cpp uses code contained in myclass.h present in the same directory as filename.cpp. Assume that the class that g++ says is undefined is contained in myclass.h and defined in myclass.cpp. So, to correctly include myclass.h within filename.cpp, do the following:

  1. In filename.cpp:

#include <iostream>
#include <myclass.h>
..source code.

  1. In the makefile:

    filename.o: myclass.C myclass.h filename.cpp
    g++ -I./ -c filename.cpp -o filename.o

    myclass.o: myclass.C myclass.h
    g++ -c myclass.C -o myclass.o

In the above, note the use of -I. option when compiling filename.cpp. The -I<directory> asks g++ to include the path following the -I part into the search path. That way myclass.h is correctly included.

In the absence of more information (the source maybe), it is difficult to say with any accuracy where the problem lies. All attempts will be but stabs in the dark.



回答4:

I assume that you have declared a member function (usually in a .h or .hpp file) but have ommited the respective definition of the member function (usually in a .cpp file).

In c++, it is possible to declare a class like so:

class foo {
  void x();
  void y();
}

with a cpp file that goes like so

void foo::x() {
   do_something()
}

Note, there is no foo::y().

This poses no problem to the compiling/linking process as long as the member function foo::y() is referenced nowhere throughout the compiled code.