C Program Compilation Stages

C Program Compilation Stages, There are 4 stages of the compilation process.
- Pre-processor
- Compiler
- Assembler
- Linker

1. pre-processor:
C Program Compilation Stages, The job of the pre-processor is to take source files as input and pre-processor file example.i produced. If we go through the example.i file we will notice that stdio.h header file will be replaced with its full code.
gcc -E example.c -o example.i
#vi example.i

2. compiler:
The job of the compiler is to take pre-processor output as an input and assembler file example.s produced. The output present in example.s is assembly level instructions.
gcc -S example.i -o example.s
#vi example.s

3. assembler:
The job of the assembler is to take compiler output as an input and an intermediate file example.o produced. this file is also known as a relocatable file. the output of this stage is a machine-level file (examle.o). So we cannot view the content of it through the editor. This can be view through a special tool call objdump.
gcc -c example.s -o example.o
objdump -D example.o | more

4. linker:
The job of the linker is to take assembler output as an input and an executable file example produced. This is the final stage at which all the linking of function calls with their definitions is done. The linker also does some extra work; it adds some extra code to our program that is required when the program starts and when the program ends. We call this code a runtime code.
gcc example.o -o example
The executable file can be viewed through a special tool called readelf. we can use the file command To check whether a file is an ELF file.


Recent Comments