C compilation — preprocessor, compiler, assembler, linker
5.1.1· Coding › C Programming
Compilation mein stages KYU hote hain?
CPU sirf raw bytes (opcodes) samajhta hai. C source mein human ke liye bahut saari cheezein hain: #include, macros, comments, main, printf jaisi names. Yeh gap bahut bada hai, isliye hum divide and conquer karte hain — har tool ek hi sub-problem solve karta hai:
- Preprocessor → text substitution handle karta hai (
#-directives). - Compiler → real C ko tumhare CPU ke liye assembly mein translate karta hai.
- Assembler → assembly ko machine code mein convert karta hai (ek object file).
- Linker → object files + libraries ko ek executable mein jod deta hai.

Stage 1 — Preprocessor (cpp)
YEH KYA KARTA HAI:
#include <stdio.h>→ us header file ka literally poora content wahan paste kar deta hai.#define PI 3.14→ macro: harPItoken ko3.14se replace karta hai.#ifdef / #ifndef / #endif→ conditional compilation (code tab hi include karo jab ek macro defined ho).- Saare comments strip kar deta hai.
DEKHNE KA TARIKA:
gcc -E hello.c # output preprocessing ke baad ruk jaata haiStage 2 — Compiler (cc1)
YEH KYA PRODUCE KARTA HAI: ek .s text file jisme human-readable assembly mnemonics hain (mov, add, call).
DEKHNE KA TARIKA:
gcc -S hello.c # hello.s produce karta haiStage 3 — Assembler (as)
OBJECT FILE MEIN KYA HOTA HAI:
- Tumhare functions ka machine code.
- Ek symbol table jo yahan defined names (jaise
main) aur woh names jo chahiye hain lekin abhi nahi mile (jaiseprintf) — undefined mark karke — list karta hai.
DEKHNE KA TARIKA:
gcc -c hello.c # hello.o produce karta hai (abhi runnable nahi!)Stage 4 — Linker (ld)
YEH KYA SOLVE KARTA HAI:
- Har undefined symbol (
printf) kolibcmein uski defined location se match karta hai. - Saare code/data sections merge karta hai aur final memory addresses assign karta hai.
- Startup code (
_start) add karta hai jomainko call karta hai.
Forecast-then-Verify
Recall Padhne se pehle har flag ka output predict karo
gcc -E ::: preprocessed source (.i)
gcc -S ::: assembly (.s)
gcc -c ::: object file (.o), no linking
gcc (koi flag nahi) ::: full executable
Har jagah ruka? Ek chhoti file par run karke verify karo.
Common Mistakes (Steel-manned)
Mnemonic
Feynman: ek 12-saal ke bacche ko explain karo
Recall Click to read
Socho tum ek robot chef ko recipe de rahe ho jo sirf button-presses samajhta hai.
- Preprocessor = tumhara assistant jo koi bhi borrowed recipe pages paste karta hai jinka tumne reference diya tha ("cake recipe dekho") aur shorthand jo tumne define ki thi copy karta hai.
- Compiler = tumhari English recipe ko robot ki step-by-step instruction language mein translate karta hai.
- Assembler = un instructions ko exact button-press codes mein convert karta hai jo robot padhta hai.
- Linker = agar tumhari recipe kehti hai "Grandma's frosting use karo," toh yeh jaata hai Grandma ke frosting steps lata hai aur staple karta hai, taaki robot ke paas ek complete booklet mein sab kuch ho jo woh start to finish run kar sake.
Active-Recall Flashcards
What are the 4 stages of C compilation in order?
Which stage handles #include and #define?
What does the compiler output?
.s), specific to the target CPUWhat does the assembler output and what's special about it?
.o) with machine code + a symbol table containing undefined referencesWhy can't you run a .o file directly?
printf) and no entry-point setup; the linker must resolve them firstWhich stage produces an "undefined reference" error?
Do header files contain function code?
gcc flag to stop after preprocessing?
gcc -Egcc flag to stop after compiling (get assembly)?
gcc -Sgcc flag to stop after assembling (get .o, no link)?
gcc -cWhy wrap macro parameters in parentheses?
What is relocation (linker)?
What's the file trail of the pipeline?
Connections
- C Preprocessor Directives
- Header Files and #include guards
- Static vs Dynamic Linking
- Object Files and Symbol Tables
- Makefiles and Incremental Builds
- Assembly Language Basics
- Compiler Optimization Levels (-O0..-O3)