Compilation process in c – Behind the Scenes

Spread the love

The process of translating source code (taken as input) to machine code or object code is called Compilation. It is done with the help of the compiler. The compiler checks source code for any syntactical or structural errors and generates object code.

There are four stage in a C program compilation process:-

  1. Pre-processing
  2.  Compiling
  3.  Assembling
  4.   Linking
c-compilation-process

Pre-processing:- This is the first phase through which source code is passed. preprocessor takes the source code as an input and it performs following task.

  1. Remove comments from the source code.
  2. Macro expansion.
  3. Expansion of included header files.

 The source code file is given an .c extension. After pre-processing it generates a temporary file with .i extension. Since, it inserts contents of header files to our source code file. Pre-processor generated file is larger than the original source file.

By executing below command, We get the all intermediate files in the current directory along with the executable.

$gcc –Wall –save-temps filename.c –o filename

Compiler:-  The code which is expanded by the preprocessor <filename>.i  is passed to the compiler and compiler converts this code into assembly code.  Compiler genrates intermediate compiled output file filename.s. It is assembly version of our source code. The source code is the code which is written in a text editor and filename has given an extension .c.

Assembling:- Assembler converts assembly code into object code.  The filename.s is taken as input and turned into filename.o. This file (filename.o) contain machine level instructions. In this stage only existing code is converted into machine language, the function calls like printf() are not resolved. 

** <file-name.o>  generated in Linux or <file-name.obj> generated in Windows.

Linking:-   Linker performs the final task of compilation process. It accepts the intermediate file <file-name.o> generated by the assembler and links all the function calls with their original definition.

** <a.out>  generated in Linux or <filename.exe> generated in Windows.

Disclosure: This website is supports by its reader. This means, if you buy any product / service through link on this website, we may earn a small commision withought costing you extra. Learn how you can support Us and why we need this?