Professional Documents
Culture Documents
Compilation Process
Compilation Process
1. Preprocessing
During compilation of a C program the compilation is
started off with preprocessing the directives (e.g., #include
Fig 1.0 Process of compilation. [1] and #define). The preprocessor (cpp - c preprocessor) is a
separate program in reality, but it is invoked automatically
To understand this process in detail, let us take an example by the compiler. For example, the #include
of a simple program written in C. <stdio.h> command in line 1 ofhelloworld.c tells the
Kernighan and Ritchie (K & R) in their classic book on C preprocessor to read the contents of the system header
programming language acquaint readers to C language by filestdio.h and insert it directly into the program text. The
compiling and executing "Hello World!" C program as result is another file typically with the .i suffix. In practice,
follows. [2] the preprocessed file is not saved to disk unless the -save-
temps option is used.
#include <stdio.h> This is the first stage of compilation process where
int main() preprocessor directives (macros and header files are most
{ common) are expanded. To perform this step gcc executes
printf("hello, world!\n"); the following command internally.
} [root@host ~]# cpp helloworld.c > helloworld.i
The result is a file helloworld.i that contains the source code
with all macros expanded. If you execute the above 4. Linking
command in isolation then the filehelloworld.i will be saved This is the final stage in compilation of "Hello World!"
to disk and you can see its content by vi or any other editor program. This phase links object files to produce final
you have on your Linux box. executable file. An executable file requires many external
resources (system functions, C run-time libraries etc.).
2. Compilation Regarding our "Hello World!" program you have noticed
In this phase compilation proper takes place. The compiler that it calls the printf function to print the 'Hello World!'
(ccl) translateshelloworld.i into helloworld.s. message on console. This function is contained in a separate
File helloworld.s contains assembly code. You can explicitly pre compiled object file printf.o, which must somehow be
tell gcc to translate helloworld.i to helloworld.s by executing merged with our helloworld.o file. The linker (ld) performs
the following command. this task for you. Eventually, the resulting file helloworld is
[root@host ~]# gcc -S helloworld.i produced, which is an executable. This is now ready to be
The command line option -S tells the compiler to convert the loaded into memory and executed by the system.
preprocessed code to assembly language without creating an During the whole compilation process there are other files
object file. After having created helloworld.s you can see the also in role along with the source code file.
content of this file. While looking at assembly code you may
note that the assembly code contains a call to the external III. SUMMARY
function printf. In this tutorial we explained compilation and execution
process steps and stages of C program in Linux using GCC.
3. Assembly Various phases during compilation and execution process of
Here, the assembler (as) translates helloworld.s into a C program take place, such as, pre-processing,
machine language instructions, and generates an object compilation, assembly, and linking.
file helloworld.o. You can invoke the assembler at your own
by executing the following command. REFERENCES
[root@host ~]# as helloworld.s -o helloworld.o [1]http://blog.techveda.org/understanding-a-c-programs-
The above command will generate helloworld.o as it is compilation-process/
specified with -o option. And, the resulting file contains the [2]http://cs-fundamentals.com/c-programming/how-to-
machine instructions for the classic "Hello World!" compile-c-program-using-gcc.php
program, with an undefined reference to printf.