The Compilation Process
gcc is the C and C++ compiler developed by GNU project.
A program is compiled by the GNU compiler Collection (GCC) and it’s based on a multi-stage process, more specifically on 4 principales stages :
1. Preprocessing :
The compilation begins with pre-processing of source file. Pre-processor is a small software that accepts C source file and performs below tasks :
- Remove comments from the source code.
- Macro expansion.
- Expansion of included header files.
To print the result of the preprocessing stage, pass the
-E option to GCC
2. Compilation of pre-processed file
In next phase of C compilation the compiler comes in action. It accepts temporary pre-processed
<file-name>.i file generated by the pre-processor and performs following tasks.
- Check C program for syntax errors.
- Translate the file into intermediate code i.e. in assembly language.
- Optionally optimize the translated code for better performance.
you can use GCC with -c option to compile and assemble without linking or -s to compile only the pre processed file
During this stage, an assembler is used to translate the assembly instructions to object code also named machine code( .o files). The output consists of actual instructions to be run by the target processor.
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. The final step is based on linking the libraries and C files. The result of this stage is the final executable program.
If it’s running without options,
cc will name this file
a.out. Otherwise To name the file something else, pass the
-o option with gcc