Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linking files in g++

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.

like image 578
E.O. Avatar asked Jun 30 '11 09:06

E.O.


People also ask

How do I link a file in Google Drive?

To link to a file stored in Google DriveOpen a record, select the Related subtab, and scroll down to the Files section. Click the Google Drive icon to open the file picker. Search and/or select the files and folders you would like to link to the Insightly record and then click the Select button.

How do I link a folder to my G drive?

On your computer, go to drive.google.com. Open or create a folder. To upload files and folders, drag them into the Google Drive folder.


2 Answers

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.

like image 60
Mario Avatar answered Sep 28 '22 06:09

Mario


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.

like image 36
Rémi Avatar answered Sep 28 '22 05:09

Rémi