Compilation Stages for C Program

Compilation process:

Compilation is a process of converting human readable code into machine understandable code

Compilation process is divided into 4 stages/parts:

  1. Pre-processor stage
  2. Translator stage
  3. Assembler stage
  4. Linker stage

1. Pre-processor stage

  • First stage of compilation process.
  • In this stage we will get pure C file or extended c file from our source file.

       Pre-processor stage is responsible for:

  • Including header files.
  • Remove comments.
  • Replace macros.
  • Do conditional compilation.

2. Translator stage

  • In this stage the code is converted into assembly language.

      Translator tasks are:

  • To generate assembly code.
  • Check for any syntactical error.

3. Assembler stage:

  • In this stage the assembly language code is converted into machine understandable code or object code.
  • The content of object file is in binary format, to make this file readable you will have to disassemble it.

4. Linker stage:

  • Linker stage is the last stage of the compilation process.
  • After completion of this stage the exe file is generated.
        Linker tasks are:

  • Linking the libraries.
  • Searching and linking function definitions (pre-defined or user defined).

For example: 

Consider, our file name is hello.c

  • To compile till preprocessor stage

            cc -E hello.c -o hello.i

  • To check output of preprocessor stage

            vi hello.i

  • To compile till translator stage

            cc -S hello.i -o hello.s

  • To check output of the translator stage

            vi hello.s

  • To compile till assembler stage

            cc -c hello.s -o hello.o

  • To check the assembler stage output

            vi hello.o

  • The output file of assembler stage is in binary non readable to dissemble it

            objdump -D hello.o

  • To compile till linker stage and generate .exe file at a time

            cc hello.c



Comments

Popular posts from this blog

Basic Structure of C Program