The process of translating source code (taken as input) to machine code or object code is called Compilation. It is done with the help of the compiler. The compiler checks source code for any syntactical or structural errors and generates object code.
There are four stage in a C program compilation process:-
Pre-processing:- This is the first phase through which source code is passed. preprocessor takes the source code as an input and it performs following task.
- Remove comments from the source code.
- Macro expansion.
- Expansion of included header files.
The source code file is given an .c extension. After pre-processing it generates a temporary file with .i extension. Since, it inserts contents of header files to our source code file. Pre-processor generated file is larger than the original source file.
By executing below command, We get the all intermediate files in the current directory along with the executable.
$gcc –Wall –save-temps filename.c –o filename
Compiler:- The code which is expanded by the preprocessor <filename>.i is passed to the compiler and compiler converts this code into assembly code. Compiler genrates intermediate compiled output file filename.s. It is assembly version of our source code. The source code is the code which is written in a text editor and filename has given an extension .c.
Assembling:- Assembler converts assembly code into object code. The filename.s is taken as input and turned into filename.o. This file (filename.o) contain machine level instructions. In this stage only existing code is converted into machine language, the function calls like printf() are not resolved.
** <file-name.o> generated in Linux or <file-name.obj> generated in Windows.
Linking:- Linker performs the final task of compilation process. It accepts the intermediate file <file-name.o> generated by the assembler and links all the function calls with their original definition.
** <a.out> generated in Linux or <filename.exe> generated in Windows.