[Univ of Cambridge] [Dept of Engineering]
next up previous contents
Next: Variables and Literals Up: ANSI C for Programmers Previous: Introduction

Compilation Stages


 
Figure 1: Compilation Stages
 

First the Preprocessor cpp is run. This strips out comments and interprets directives (lines with a `#' character in the first column). The `#include' directive read in the named file, looking for the file in the directory /usr/include. Include files have a `.h' suffix by convention, and shouldn't contain executable code, only definitions and declarations. /usr/include/stdio.h and /usr/include/stdlib.h should always be included. Other useful include files are /usr/include/limits.h and /usr/include/math.h which define characteristics of the machine and the maths implementation. Further preprocessor directives to do with macros, etc, will be introduced later. If you want to see how the code looks after pre-processing, try typing cc -Aa -E basics.c

After the preprocessor comes the compiler, which after a pass or 2 produces assembler code. This is assembled to produce an object file, in this case basics.o.

Finally the link-loader produces the executable from the object file and any other object you mention. The standard C library is automatically consulted. Any other libraries that your code needs have to be mentioned on the compile line. E.g., ending the line with `-lm' links in the maths library, `-lX11' links in X graphics functions. Note that it's not sufficient to just have `#include <math.h>' at the top of the file if you're doing maths. This just supplies enough information so that the compiler can do its work correctly. It doesn't tell the link-loader where the maths routines actually are. You need to have `-lm' on the command line before an executable can be produced.

The stages of compilation can be seen if a `-v' flag is given to the compiler.


next up previous contents
Next: Variables and Literals Up: ANSI C for Programmers Previous: Introduction
Tim Love
1999-10-06