Q6. Compiler Behind the scene. How does compilation work. How many phases are there in Compilations? PreProcessor, Compiler, Assembler, Linker and Loader.
Program: 150
Compilation:
The Compilation is a process of converting the source code or high level code into object code or machine code (binary 0 and 1) so that the program can run on machine.
There are four phases for a C program to become an executable:
- Pre-Processing
- Compiling
- Assembly
- Linking
1. Pre-Processing (Pre-Processor):
Input: file.c
Output: file.i
First phase of compilation. It checks the source code syntax and structure, if source code is error-free, then it perform the four basic functionality given below:
- Comments Removal
- Expansion of MACRO
- Expansion of Included Files
- Conditional Compilation
2. Compiling (Compiler):
Input: file.i
Output: file.s
Second phase of compilation. It converts expanded code into the intermediate compiled code having assembly level instructions (assembly code).
3. Assembly (Assembler):
Input: file.s
Output: file.obj (DOS) or file.o (UNIX)
Third phase of compilation. It converts the assembly code into object code by using assembler.
Extension of output file depends upon operating system.
For DOS (Disk Operating System), it will create file.obj
For UNIX operating system, it will create file.o
4. Linking (Linker):
Input: file.obj (DOS) or file.o (UNIX)
Output: file.out or file.exe
Fourth phase of compilation. It converts object code into machine code by using linker. It combines the object code of library files with object code of our program.
For example: If we have printf() function in our program then it will add its associated code into our program object code.
Extension of output file depends upon operating system.
For DOS (Disk Operating System), it will create file.exe
For UNIX operating system, it will create file.out
5. Loading (Loader):
The loader loads the executable file for the execution.