The C language program we edit cannot be directly executed on a machine. It is just a file with the “.c” extension (also known as source code) and needs to be processed to convert it into an executable file that can be run on the machine. This process of processing C language is called compilation and linking.
Compilation is the process of translating text-based source code into machine language form of object files.
Linking is the process of organizing object files, operating system boot code, and library files used to form the final executable code.
The process of compilation and linking is illustrated in the following diagram:
As shown in the diagram, the entire code compilation process is divided into four processes:
- Pre-processor
- Compiling
- Assembly
- Linking
1. Pre-processing
During the pre-processing step of a C program compilation process, the processor does some initial processing in which it resolves all the lines starting with special characters. For instance, all the lines in your code starting with the pound (#) character which is known as macros or #define directives.
For More information about Pre-processing, please check How Does Preprocessor Work in C ?
It is the best to read it after we have studied Macro and Head Files.
2. Compiling
Compilation is the process of reading the source program (character stream), performing lexical and syntax analysis on it, and converting high-level language instructions into functionally equivalent assembly code. The task of a compiler program is to perform lexical analysis and syntax analysis, and after confirming that all instructions comply with the syntax rules, translate them into equivalent intermediate code representations or assembly code.
3. Assembly
Assembly refers to the process of translating assembly language code into target machine instructions. For each C language source program processed by the compilation system, the corresponding object file is obtained after this process. The object file contains the target machine language code equivalent to the source program. The object file is composed of segments. Typically, an object file has at least two segments: a code segment and a data segment.
4. Linking
The main task of the linker program is to link related object files together, that is, to connect symbols referenced in one file with their definitions in another file, so that all these object files become a unified whole that can be loaded and executed by the operating system. This step typically involves adding in any libraries that are required.