This page gives basic information on how to compile your program with the gcc or g++ compiler: an overview of the compilation process, some example commands, and the make utility.
Overview:
Compiling a program is the second step (after writing the code) of creating a runnable program. The first part of the compilation process (Preprocessing) checks the code for syntatic or other types of errors (different compilers have different capabilities). If there are problems, the compilation process terminates after this step, and a good compiler will give you details about the errors it found.
If there are no errors, then the compiler compiles and assembles the file, creating an object file, recognized by its .o extension. This is not a runnable program, but a file that can be used to create a runnable program.
The next step is to link the .o file to any dependencies (other of your .o files, other external files). This step is called linking. The result is your runnable code.
The source code should have the extension .cpp (my favorite, short for c plus plus), or .c++ (harder to type) or .cc or .cxx or a couple of others. (see the manual page by typing 'man g++ or man gcc')
Header files should be labeled .h, and will not be compiled or linked.
The default output creates an executable called a.out. You can specify a name by using the -o switch (variable used with command eg g++ -o) so gcc -o myprog will create a program executable named myprog.
The compiler is invoked with the command g++ and the name of the files that are needed. gcc is basically the same command, but g++ specifies the compiler focus on C++ and ensures that C++ libraries are included, so for this class, g++ is probably the best bet.
Examples:
Basic command: Say we have a source file called source.cpp and a header file called source.h. You will need to be in the directory in which these files are located.
Creating an executable called a.out: g++ source.cpp. Note that you do not include the .h file. This command creates the object file and then links it and creates the executable a.out.
Naming your executable. Use the -o switch to name the output file (executable).
Creating a file called myprog.exe: g++ source.cpp -o myprog.exe. Note that the -o comes after the source file.
Getting all warnings: Use the switch -Wall
g++ -Wall source.cpp This will cause any warnings that the compiler finds to be listed. This is important, as your program may compile even with warnings, but you should NOT have warnings, as they show you are not using something correctly. You will be deducted if there are warnings.
Debugging: Use the -g switch
g++ -g source.cpp. for more information on the debugger, type man g++. or see this.
Multiple files: you can compile programs that include more than one source file. If your files are source1.cpp and source2.cpp:
g++ source1.cpp source2.cpp
Combining things. You can do all of the above at once.
g++ -g -Wall source1.cpp source2.cpp -o myprog. This will create an executable called myprog from the 2 source code files, list all warnings and invoke the debugger.
Make
The make utility is really just a way to automate the process. You create a file, which by default must be called Makefile, which contains all the commands you need to compile your program. Make is a pretty powerful utility, but the basic functions are pretty simple.
In creating a makefile, you can control the process. It helps to break the process down so that each piece can be done separately, or not done, )if nothing is needed to update).
Example makefile:
Assume we have a program with 3 source files, each with a header file, call them s1.cpp, s1.h, s2.cpp, s2.h, s3.cpp, s3.h.
To create an executable called myprog, create a file named Makefile like this:
myprog: s1.o s2.o s3.o
<tab>g++ s1.o s2.o s3.o -o myprog
s1.o: s1.cpp s1.h
<tab>g++ -c s1.cpp
s2.o: s2.cpp s2.h
<tab>g++ -c s2.cpp
s3.o: s3.cpp s3.h
<tab>g++ -c s3.cpp
Notes: don't type the word tab, but use a tab to tab over
The usual form of a Makefile entry is:
target: dependency ...
(tab)command
(tab)command
....
MAKE is very particular about how targets, dependencies, and commands are arranged
in Makefile entries. The target should be at the leftmost margin with a colon
immediately after it. There should be at least one space or tab between the
colon and the first dependency. A dependency is a file that will be needed by
the compiler to perform the command that comes next. Make uses the dependency
list to decide if something needs to be updated. Dependencies should be separated
by spaces or tabs. The command(s) should be on the next immediate line and spaced
from the left margin by one and only one tab. In general, the Makefile tells
MAKE how to do each step of the compilation process. For each .cpp file, MAKE
creates a corresponding .o file. Then when all the .o files have been compiled,
MAKE links them together into an executable.
Once the Makefile is done, simply type make, or make myprog (the taget label). If you have a complicated makefile, make <target> is useful, but for a simple one, make will simply start at the top of the file.
Make starts at the first line (or target line) and uses the dependency list to determine if s1.o, s2.o or s3.o need to be updated. It first goes to s1.o to see. It sees that s1.o depends of s1.cpp and s1.h. If these have been changed, it will perform the command g++ -c s1.cpp to create the object file s1.o. The first time you run make s1.o will not even exist, so it will have to do this, but it will check every time to see if all dependencies are up to date. Simarly, s2.o and s3.o are created. If you only change s1.cpp, only s1.o will need to be recompiled, so this speeds things up.
note: g++ -c command creates a .o file, but does not link anything.
Thanks to UCSD computing services for valuable information.