The C Compilation Process
Exemple of a C script:
The four stages of compiling a C program:
Compiling a C program is a multi-stage process. At an overview level, the process can be split into four separate stages:
Preprocessing, compilation, assembly, and linking.
The first stage of compilation is called preprocessing. In this stage, lines starting with a
# character are interpreted by the preprocessor as preprocessor commands. These commands form a simple macro language with its own syntax and semantics. This language is used to reduce repetition in source code by providing functionality to inline files, define macros, and to conditionally omit code.
Before interpreting commands, the preprocessor does some initial processing. This includes joining continued lines (lines ending with a
\) and stripping comments.
To print the result of the preprocessing stage, pass the
-E option to
gcc -E filename.c
In this stage, the preprocessed code is translated to assembly instructions specific to the target processor architecture. These form an intermediate human readable language.
The existence of this step allows for C code to contain inline assembly instructions and for different assemblers to be used.
To save the result of the compilation stage, pass the
-S option to
gcc -S filename.c
During this stage, an assembler is used to translate the assembly instructions to object code. The output consists of actual instructions to be run by the target processor.
To save the result of the assembly stage, pass the
-c option to
gcc -c filename.c
The object code generated in the assembly stage is composed of machine instructions that the processor understands but some pieces of the program are out of order or missing. To produce an executable program, the existing pieces have to be rearranged and the missing ones filled in. This process is called linking.
The linker will arrange the pieces of object code so that functions in some pieces can successfully call functions in other ones. It will also add pieces containing the instructions for library functions used by the program. In the case of the “Hello, World!” program, the linker will add the object code for the
The result of this stage is the final executable program. When run without options,
gcc will name this file
a.out. To name the file something else, pass the
-o option to
gcc -o filename filename.c
NB° GCC (GNU Compiler Collections):
Is a set of compilers created by the GNU Project. GCC is free software able to compiling various programming languages, including C, C ++, Objective-C, Java, Ada, Fortran, and Go.
GCCis GNU C (language) compiler. It's used to "convert" programs written in C programming language into binary executable on computer.