The C Compilation Model
- The Preprocessor
- We will study this part of the compilation process in greater
detail later
- Removes comments
- Interprets special preprocessor directives
denoted by #.
- Run the compiler with the -E option to see the
preprocessor output.
- C Compiler
- The C compiler translates source to assembly code.
- The source code is received from the preprocessor.
- Run the compiler with the -S option to leave
the assembler output in a .s file.
- Assembler
- The assembler creates object code.
- Run the compiler with the -c option to leave
the object code in a .o file.
- Link Editor
- Takes one or more object files (.o) and library files
(.a and .so) and creates an executable.
- If a source file references library functions or functions
defined in other object files the link editor combines
these functions (with main())
- External variable references resolved here also.
- Compiler options to control the phases
- -E
- Run just the preprocessor
- -S
- Stop after producing assembler code in a .s file.
- -c
- Suppress the linking process and produce a .o file
for each source file listed. Several can be subsequently
linked by the gcc command, for example:
prompt> gcc file1.o file2.o ...... -o executable
Some Useful Compiler Options
- -Wall
- Do serious checking of the code, kinda like lint.
- -o file
- Send the compiled output to file rather than the
default a.out.
- -O level
- Optimize the compiled code to level, which is in the
range 0 to ... (depends on compiler).
- -I directory
- Add pathname to the list of directories in which to search
for #include files with relative filenames.
The preprocessor first searches for #include files in
directories named with -I options (if any), and then
in /usr/include. So to include header files stored in
/home/myname/myheaders you would do:
prompt> gcc prog.c -I /home/myname/myheaders
- -l library
- Link with object libraries. This option must follow the source
file arguments. The object libraries are archived and can be
standard, third party or user created libraries.
Probably the most commonly used library is the math library.
You must link in this library explicitly if you wish to use
the maths functions (don't forget to
#include <math.h>). For example:
prompt> gcc calc.c -o calc -lm
- -L directory
- Add directory to the list of directories containing libraries.
The linker always looks for standard and other system libraries
in /lib and /usr/lib. If you want to link in
libraries that you have created or installed yourself (unless
you have certain privileges and get the libraries installed
in /usr/lib) you have to specify where you files are
stored, For example:
prompt> gcc prog.c -L /home/myname/mylibs -lmystuff
- -g
- Invoke debugging option. This instructs the compiler to produce
additional symbol table information that is used by a variety
of debugging utilities.
- -D
- Define symbols either as identifiers (-D
identifer) or as values (-D
symbol=value) in a similar fashion as the
#define preprocessor command.