The Compilation Model: Preprocess, Compile, Link in C
Building a C program is a three-stage pipeline behind the single gcc command. The preprocessor handles # lines (pasting headers, expanding macros, stripping comments), the compiler translates each .c file independently into object code, and the linker stitches objects and libraries into one executable.
Stage knowledge turns cryptic errors easy: syntax and type errors come from the compiler, while undefined reference is a linker error (declared but never defined or linked). Headers (.h) declare, sources (.c) define β splitting them is what makes multi-file projects work.
Preprocessing
#include pastes headers in, #define expands macros β output is still C source, just bigger. Inspect it with gcc -E.
Compilation
Each .c file compiles separately to a .o object file. Syntax errors, type mismatches, and unknown names fail here.
Linking
The linker combines objects and libraries into the executable, resolving every call to a real definition.
Headers vs sources
Headers declare (signatures, struct layouts); .c files define (bodies). Including without defining causes linker β not compiler β errors.
// math.h: int square(int x); (declaration)// math.c: int square(int x) { return x * x; } (definition)// main.c: #include "math.h" ... square(6) ...// Build: gcc main.c math.c -o app -WallRecommended resourcesAffiliate links Β· may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C yourself β building beats watching.