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.
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.
In this phase some assembly code is generated that would do the work that the pre-processed source code is meant to do.
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.
In this phase the object code is linked with the standard and custom static libraries to create the executable code.
The loader pulls the executable instructions and any code from dynamically linked libraries into system memory.
This executable code is executed by the run-time component of the operating system.