C Compiler and Build Process
⏱ Estimated reading time: 2 min
A C compiler translates a C program written in high-level language into machine-level executable code.
The build process occurs in multiple stages.
Phases of C Compilation
The C build process consists of the following steps:
-
Preprocessing
-
Compilation
-
Assembly
-
Linking
1. Preprocessing
Handled by the preprocessor.
Tasks:
-
Expands macros (
#define) -
Includes header files (
#include) -
Removes comments
-
Handles conditional compilation
Output:
-
Expanded source file (
.i)
2. Compilation
Handled by the compiler.
Tasks:
-
Checks syntax
-
Converts preprocessed code into assembly code
-
Performs optimizations
Output:
-
Assembly file (
.s)
3. Assembly
Handled by the assembler.
Tasks:
-
Converts assembly code into machine code
Output:
-
Object file (
.o)
4. Linking
Handled by the linker.
Tasks:
-
Links object files
-
Resolves external symbols
-
Links standard libraries
-
Creates final executable
Output:
-
Executable file (
a.out/program.exe)
Complete Build Command
Role of Header Files
-
Provide function declarations
-
Enable compiler to check function usage
Static vs Dynamic Linking
| Feature | Static Linking | Dynamic Linking |
|---|---|---|
| Library | Copied into exe | Loaded at runtime |
| Size | Larger | Smaller |
| Speed | Faster | Slightly slower |
Compiler Errors vs Linker Errors
Compiler Errors
-
Syntax errors
-
Undeclared variables
Linker Errors
-
Undefined reference
-
Missing function definitions
Build Tools
-
gcc– GNU Compiler Collection -
make– Build automation -
cmake– Build system generator
Summary
-
C compilation occurs in multiple stages
-
Preprocessor runs first
-
Compiler generates assembly
-
Assembler creates object code
-
Linker produces executable
Register Now
Share this Post
← Back to Tutorials