Building a multi-file program
Let's say there are three files: mainfil.cpp, subfunc.cpp, and sub.h
that is included by both mainfil and subfunc.
Method 1: step 1: individually compile each .cpp file:
g++ -c mainfil.cpp
g++ -c subfunc.cpp
The -c option tells to compile only, not to try to create an
executable file. You could combine both in one command:
g++ -c mainfil.cpp subfunc.cpp
Either way, .o files are created: mainfil.o and subfunc.o.
Then step 2: link the .o files together into an executable file:
g++ mainfil.o subfunc.o
This creates a.out by default. To create an executable with a
g++ -o prog1 mainfil.o subfunc.o
This creates an executable file called prog1.
With many files of many dependencies, individually compiling becomes a
logistic nightmare of making sure that modifed source is recompiled
and files that depend on modifed .h files are recompiled and
everything is linked into a program executable.
So there's a better solution: use the make utility. You specify in a
file called Makefile the dependencies among the files and how to
compile the files. Then after changes to the source files, you only
have to give the make command; it compiles what's been modified and
links what needs to be linked.
Using make. Create a file called Makefile (capital M) in
the directory with the files. Its format is:
There must be a tab after the filename and at the start of the compile
Here's the Makefile for our example:
proj1: mainfil.o subfunc.o
g++ -o proj1 mainfil.o subfunc.o
mainfil.o: mainfil.cpp sub.h
g++ -c mainfil.cpp
subfunc.o: subfunc.cpp sub.h
g++ -c subfunc.cpp
Makefiles pay for themselves very quickly.
New Project. Console Application. Give it a name and save to disk
each New file will be added to current project.
and/or Add existing source files to project:
Project | Add to project... each .cpp file (NOT .h files)
Compile and Run project.
Using DJGPP rhide:
rhide is "project-oriented" in that it thinks all the files loaded
into it are part of one program so you only have to load all the .cpp
files into rhide and then Run (Ctrl-F9). This will compile them all
and link them together into one executable. For the programs we're
doing in this class that's easy enough. But for a program made up of
many source files, a Project can be created, Add Item each .cpp file
to it (don't Add the .h files). Then Run will load and compile
the necessary files.
in File menu,
Win32 Console Application,
give it a name and location.
then Empty Project.
then Project menu,
Add to Project,
and choose the .cpp files (not .h files).
A former student wrote this. I can't remember who, though.
Here's a setup that I used with Visual c++ version 5.0 that should
work with 6.0. The key is to select a win32 console application under
project instead of the default which assumes you want to use MFC
(wonderful Microsoft). Here's the setup:
NEW -> workspace -> CMIS240 (for example)
NEW -> project -> win32 Console Application
ALSO Select Add to current workspace
Next under your new project
select NEW FILE and use the C++ source file and give it a file name.
Using Turbo 3.0:
Project menu, Open project, create some .PRJ file in the directory
where the files are. Add Item for each .cpp file (i.e. mainfil.cpp
and subfunc.cpp) but NOT for the .h files. Compiling will compile
each modified file and link together into an executable.
Using Borland 4.5:
Maybe Borland 5 is similar?
Thanks to Matt Vogel for researching this.
Project menu, choose New project. In pop-up window, give name for
the project. Use name of file containing main(). Select
EasyWin[.exe] as the Target Type.
- In the Project window, right click on the top file[.exe]. Add node
for each .cpp that makes up the project.
- Delete the two extra files that the compiler supplies (.rc and
.def). If you delete them the compiler does not ask for them. Delete
by right mouse button, choose Delete node.
- To compile the project make sure to choose the Make all option from
the Project menu at the top. If you click on the project.exe in the
project window the compiler will attempt to compile the project and
give you errors.
- Once the project has been complied and built with 0 errors do not
run the project from inside the compiler. It will give you an error.
Instead find the .exe file that the compiler made using Windows
Explorer and double click on it to run the program. You can also run
the program from the DOS prompt.