Compiling a class... command to do so.

I am compiling a class.

The files are as follows.
file1.cpp   // PROGRAM
file2.cpp   // class/methods  
file3.h     // .h file

Using the g++ compile what command would I use at the linux prompt to correctly compile this program.
g++ -o program file1.cpp file2.cpp file3.h     <---- ??

Or do I need to link them (-L) somehow?

Thanks for the help.

Ben
killer455Asked:
Who is Participating?
 
KashraConnect With a Mentor Commented:
You're almost right. When you use a command line compiler like g++, you have to list all your implementation files (*.cpp) on the command line. Those *.cpp files should have #include headers in each of them that refer to the approriate *.h header files.

So, say file1.cpp requires file3.h and iostream:

// file1.cpp
#include <iostream>
#include "file3.h"

Say file2.cpp requires file3.h also:
// file2.cpp
#include "file3.h"

file3.h should have some preprocessor commands to keep it from being included twice:
// file3.h
#ifndef FILE3
#define FILE3
/* CODE FOR file3.h */
#endif

And finally, your g++ command line might look like:
g++ -o output file1.cpp file2.cpp

Notice the lack of header files, the compiler should take care of that. Files #included with quotation marks should be in the same directory as you're compiling from, while files with <> around them should be in the standard directories the compiler looks in.
0
 
DarthNemesisCommented:
your file1.cpp should have an #include "file3.h" and #include "file2.cpp" at the top.
0
 
killer455Author Commented:
Thanks a bunch, just what i needed.
0
All Courses

From novice to tech pro — start learning today.