PDA

View Full Version : using g++



diamantis13
September 26th, 2007, 06:13 PM
Hi everybody!

I have a very simple (and a bit silly) question. I have a main function in a file test.cpp and a couple of classes in files class.h and class.cpp
before writing the classes in separate files i just did:
$ g++ test.cpp -o test.exe
what should I do?

Thank you very much

LaRoza
September 26th, 2007, 06:15 PM
You can include them:



#include "class.cpp"


class.cpp should include the headers. Use inclusion guards!

You compile as normal, the preprocessor will do the rest.

hod139
September 26th, 2007, 06:16 PM
g++ test.cpp class.cpp -o test

Alternatively, you can learn about makefiles.


Also, in linux, you do not need the .exe extension, that is a dos thing.

hod139
September 26th, 2007, 06:20 PM
You can include them:



#include "class.cpp"
class.cpp should include the headers. Use inclusion guards!

You compile as normal, the preprocessor will do the rest.

You should not get into the habit of including cpp files in other cpp files. Except for very esoteric situations, you only want to include header files. (This is why include guard exist only in headers, cpp file are not usually included). You compile each cpp file separately, and let the linker worry about putting it all together at the end. This is not the compiler's/programmer's job to do that by playing with the includes.

diamantis13
September 26th, 2007, 06:27 PM
I used:

g++ test.cpp class.cpp -o test

and it worked :lolflag:

Thank you very much