TDDD33 Programming (C++)
Compilation
Compile your programs
To transform your programs to machine code the computer can
understand, you must compile the source code. The compilation is a
lengthy process of feeding the source code through several programs,
each performing one part of the transformation. The steps involved are
outlined below. Fortunately, all you need to know to perform the steps
are one (comparatively) simple command. We assume your source code is
stored in a file called my_superior_program.cc and you want the
ready program to be named super.
g++ -Wextra -std=c++98 -g my_superior_program.cc -o super
You can make "-Wextra -std=c++98 -g" default options by adding an alias in your".cshrc.private" file. Add a line "alias g++ 'g++ -Wextra -std=c++98 -g'".
You just need to replace my_superior_program.cc and
super above with whatever names you choose. Finally, to
run the program, type the name (the one you choose):
superTo be sure you are executing your program and not an equally named system or built-in command you type the slightly more inconvenient:
./super
The compilation steps
When you execute the command g++ the following steps are
performed in order.
Preprocessor (cpp)
The preprocessors reads your source code and adds, replaces or removes certain parts according to preprocessing instructions you can add in your code. The most common preprocessing directive you use is #include. The output from the preprocessing step is new source code.
Compiler (g++)
The compiler reads the output from the preprocessor and verify the code is valid C++ without any syntax errors. The high level instructions are converted to low level assembler instructions closely matching the capabilities of the processor. The output is an assembler program, still readable by humans.
Assembler
The assembler converts the assembler code from the compilation step to binary instructions (machine code) that processor can understand.
Linker (ld)
The linker reads several pieces of the program and libraries you used and put them together to form a complete program, and arranges the final program memory layout.
C++ compilers at IDA
You may find this page on compilers at IDA useful.
It is written by Tommy Olsson, He is a senior programmer/teacher at the department (IDA) and has an excellent collection on C++ and programming information, unfortunately mostly available in Swedish.
Make
Larger programs contain several source files. Compiling every file
whenever the program is to be built is tedious and time consuming. Only
the source files that was changed should need to be recompiled. But
keeping track of which files was changed is even harder. To aid this
task we use make. It is a program that reads a file
containing instructions (rules) of how and in which order files should
be processed. This file is named Makefile and most often
reside in the same directory as the source code. A basic
Makefile, containing the rules needed to build our
super program in two steps would contain:
CXX=g++
CXXFLAGS=-Wextra -std=c++98 -g
super : my_superior_program.o
$(CXX) $(CXXFLAGS) $^ -o $@
super.o : my_superior_program.cc
$(CXX) $(CXXFLAGS) -c $^
The first line defines a variable with the compiler we want to use. The second line define the compiler options. Saving this in variables makes it easy to change later.
The next two lines defines the first rule. It tells make
to create the file super from the file(s)
my_superior_program.o by using the following command
(after variables are expanded):
g++ -Wextra -std=c++98 -g my_superior_program.o -o super
Notice how $^ and $@ expand to
my_superior_program.o and super respectively
(determined from the previous line). Read the make manual
page to get all the details.
The last two lines describe how to create the file
my_superior_program.o from the file
my_superior_program.cc by using the command (after
variables are expanded):
g++ -Wextra -std=c++98 -g -c my_superior_program.cc
Finally a more general form of a Makefile. The
make program automatically figures out dependencies
between the rules, and whether files need to be recreated.
VARIABLE1=values
VARIABLE2=values
...
VARIABLEn=values
file1_to_create : file11_needed file12_needed ... file1M_needed
_tab_ command_to_do_it
file2_to_create : file21_needed file22_needed ... file2M_needed
_tab_ command_to_do_it
...
fileN_to_create : fileN1_needed fileN2_needed ... fileNM_needed
_tab_ command_to_do_it
N.B. _tab_ should be exactly one tabulator, not a
series of spaces.
Page responsible: Klas Arvidsson
Last updated: 2010-08-21
