Compiling a class... command to do so.

Posted on 2003-03-15
Medium Priority
Last Modified: 2010-04-01
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.

Question by:killer455
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 8144659
your file1.cpp should have an #include "file3.h" and #include "file2.cpp" at the top.

Accepted Solution

Kashra earned 80 total points
ID: 8144703
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 */

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.

Author Comment

ID: 8145747
Thanks a bunch, just what i needed.

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question