C Program Compilation Phases

The process of creating executable file from C source code uses some tools such as a pre-processor, compiler, assembler, and linker. This process involves 4 phases. Then the executable instructions are loaded into memory and executed by the operating system.

c-compilation-phases

1. Pre-processing

This phase involves 3 distinct jobs.

  • Remove the comments : The user specified comments are removed from the source code.Single line comments (// comment-till-end-of-line ) and multi-line comments (/* Comment */) are simply removed.
  • Include the header files : The #include <header_file> directives are removed and the content of that header file are included in the source code file.#include <stdio.h> : The standard io header file is included in the C source code file.
  • Substitute the macros : Programmers have the flexibility of defining Names for constants. Those names are not treated as variables, but get substituted by the constant values before compilation work starts.#define MAX 10000 : All occurrences of MAX in source will be substituted by numeric constant 10000.

2. Compilation:

In this phase some assembly code is generated that would do the work that the pre-processed source code is meant to do.

3. Assembly

Assembler takes the assembly code as input and creates object code in an object file. The object file will have an extension of .o in linux and .obj in windows platform.

4. Linking:

In this phase the object code is linked with the standard and custom static libraries to create the executable code.

5. Loading: 

The loader pulls the executable instructions and any code from dynamically linked libraries into system memory.

6. Execution:

This executable code is executed by the run-time component of the operating system.

You may also like...