The process of turning a C source file (.c) into an executable file is handled by the compiler and related tools. This process is divided into four main stages.
1. Stage 1: Preprocessing
The Preprocessor is the first step. It handles directives that begin with the pound symbol (#). It doesn’t analyze the syntax of the C code, only performs text manipulation.
- Actions:
- Includes: Replaces
#include <filename>with the actual content of that file (e.g., placing the code fromstdio.hinto your source file). - Macros: Replaces all defined macros (e.g.,
#define PI 3.14159) with their value. - Conditional Compilation: Removes sections of code based on directives like
#ifdef.
- Includes: Replaces
- Output: An expanded source file (often with a
.iextension).
To stop the compilation after the preprocessing stage:
gcc -E main.c -o main.i
2. Stage 2: Compilation
The Compiler proper takes the preprocessed code (.i file) and translates it into assembly language. This stage checks the C code for syntax errors and ensures the code is valid C.
- Actions: Translates C code into processor-specific assembly instructions.
- Output: An assembly file (often with an
.sextension).
To stop the compilation after the compilation stage:
gcc -S main.i -o main.s
3. Stage 3: Assembly
The Assembler takes the assembly code (.s file) and converts it into machine-readable binary instructions known as object code.
- Actions: Converts assembly language into machine code (binary).
- Output: An object file (often with a
.oextension). This file contains your compiled code but is not yet ready to run because it hasn’t connected to external library functions.
To stop the compilation after the assembly stage:
gcc -c main.s -o main.o
4. Stage 4: Linking
The Linker is the final and crucial step. It takes one or more object files (.o files) and links them with any necessary external libraries (like the compiled code for the printf function from stdio.h).
- Actions: Resolves external references (e.g., finding the compiled code for
printf) and combines all pieces into a single, cohesive executable file. - Output: The final executable program (e.g.,
a.outorhello).
The standard compilation command performs all four stages in sequence:
gcc main.c -o hello
