Four Phases in Compilation System – Sample with C Programming and GCC Compiler

The translation is performed by the compilation system in the sequence of four phases:

  • Preprocessor – Preprocessing
  • Compiler – Compiling
  • Assembler – Assembly
  • Linker – Linking

Preprocessing

Preprocessor will read the source file and do following actions:

  • Removes comments.
  • Macro expansion.
  • Expanding included files.

When the compiler process the source file HelloWorld.c, it will generate a text file with .i extension.
You can get this file by using following command:

gcc -E HelloWorld.c -o HelloWorld.i

Compiling

Compiler takes the output of the preprocessor and translates the file into assembly language, it also checks the C language syntax for errors.
It will generate a text file with .s extension. You can get this file by using following command:

gcc -S HelloWorld.i -o HelloWorld.s

Assembly

Assembler takes the output of the Compiler and translate it into low-level machine code.
It will generate a binary file with .o extension. You can get this file by using following command:

gcc -c HelloWorld.i -o HelloWorld.o

Linking

Linker takes the output of Assembler and merges all the object code from multiple modules into a single one.
It will generate the target executable file.

gcc HelloWorld.o -o HelloWorld

Summary

The four steps of compilation are: preprocessing, compiling, assembly, linking.
They are owned by Preprocessor, Compiler, Assembler, Linker in compilation system.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.