All rights are reserved by Niteesh Kumar.. Theme images by Storman. Powered by Blogger.


Total Pageviews

Blog Archive

Follow by Email


Tuesday, 28 February 2017

Compilation steps of C program

Compilation of a C program is a four stage process-
  • Preprocessing (creates file.i)
  • Compilation (creates file.s)
  • Assembly (creates file.o)
  • Linking (creates executable file)
Note- when we will generate all intermediate files for a cpp program preprocessing generates file.ii while other are same as C program intermediate files.

 We will discuss details of these steps later.First of all make a .c or .cpp file- 
  • Open your terminal and type this command-
gedit hello.c

     and write the following code in this file.

  • After saving this type this command in your terminal-
 gcc -save-temps hello hello.c (for .c file)

g++ -save-temps hello hello.cpp (for .cpp file)

  • Now open your folder where you have created the program and you will see that there are four other files instead of hello.c-

  • To see the output type ./hello in the terminal.


 This is the first process of compilation.This process perform the following operations-
  • Removal of comments
  • Expansion of macros (i.e. #define)
  • Expansion of all included files
Now open your terminal in the same folder where all these intermediate files are stored and type-

cat hello.i

and you will see a large file in your terminal-

Expanded stdio.h file is above the highlighted portion
(where #include<stdio.h> supposed to be) and you can see in highlighted portion that there are no comments and no #define.The value of var in a=var of main program is replaced by variable defined in macro var 5 .


This is the second process of compilation and in this the processed code is converted to assembly level instruction.
Type the command given below to see the file-

cat hello.s

this will be the output of above command-

You can see that file contains all the instructions in assembly language.


This is the third process of compilation and in this process the assembly language code is converted to machine  level code (object code). The functions call like print() are not resolved.You can see that data in double quotes of printf statement is still same in the code.
Type the command given below to see the file-

cat hello.o

this will be the output of above command-


This is the last process of compilation.In this linking of all function calls with their definition are done.Linker do some extra work and adds the object code for function calls that are missing.Like in the above program linker will add the object code for printf() function to make the above object file executable.
 We can see this change in the size of object file and executable file-

see the difference of text size.It means this process is adding some extra piece of object code.
Type the command given below to see the file-

cat hello

this will show the executable file-

 for the output type
in your terminal-

0 on: "Compilation steps of C program"