5.3.1 · HinglishBuild Systems & Toolchain

Compilation stages — preprocessing, compilation, assembly, linking

1,748 words8 min readRead in English

5.3.1 · Coding › Build Systems & Toolchain

Figure — Compilation stages — preprocessing, compilation, assembly, linking

YEH pipeline exist KYUN karti hai?


Stage 1 — Preprocessing

YEH KYA karta hai:

  • #include <stdio.h> → us header file ka poora content physically paste kar deta hai.
  • #define PI 3.14 → har PI token ko 3.14 se replace kar deta hai.
  • #ifdef / #ifndef / #endif → code ke blocks ko conditionally rakhta ya delete karta hai.
  • Comments strip kar deta hai.

ISKO kaise dekhen:

gcc -E hello.c -o hello.i   # -E = stop after preprocessing

Stage 2 — Compilation

ANDAR kya hota hai:

  1. Lexing → text ko tokens mein tod deta hai.
  2. Parsing → ek Abstract Syntax Tree (AST) banata hai.
  3. Semantic analysis → type checks (yahi se tumhari zyaadatar errors aati hain!).
  4. Optimization → jaise constant folding 2*36.
  5. Code generation → target CPU ke liye assembly emit karta hai (x86, ARM…).

ISKO kaise dekhen:

gcc -S hello.i -o hello.s   # -S = stop after compilation

Stage 3 — Assembly

ABHI runnable kyun nahi? Kyunki yeh aisi functions call kar sakta hai (jaise printf) jo doosri files mein rehti hain. Unke addresses abhi unknown placeholders hain.

ISKO kaise dekhen:

gcc -c hello.s -o hello.o   # -c = compile+assemble, stop before linking

Stage 4 — Linking

YEH kya resolve karta hai:

  • Tumhara hello.o kehta hai "mujhe printf chahiye". Linker C standard library (libc) ke andar printf dhundhta hai aur address patch kar deta hai.

Do prakar:

  • Static linking → library code executable mein copy ho jaata hai (bada file, self-contained).
  • Dynamic linking → sirf ek reference store hota hai; actual .so/.dll runtime par load hoti hai (chota file, shared).

KAISE:

gcc hello.o -o hello        # links, produces executable

Common Mistakes (Steel-manned)


Forecast-then-Verify


80/20 — The vital few



Recall Feynman: 12-saal ke bachhe ko samjhao

Socho tumne English mein ek recipe likhi. Pehle, ek helper saari sub-recipes jo tumne reference ki thi copy karke laata hai aur shorthand notes theek karta hai (preprocess). Phir ek translator use chef ki special cooking-shorthand mein rewrite karta hai (compile). Ek doosra insaan us shorthand ko exact robot-arm button presses mein badalta hai (assemble) — lekin recipe abhi bhi kehti hai "doosri kitchen se sauce lo". Aakhir mein, linker doosri kitchens se saari sauces collect karne dauda jaata hai aur sab kuch ek FINISHED cookbook mein glue kar deta hai jise robot start-to-finish run kar sake.


Connections


Flashcards

4 compilation stages order mein kya hain?
Preprocessing → Compilation → Assembly → Linking
Preprocessing ke baad kaun sa gcc flag rukta hai?
-E (.i produce karta hai)
Compilation ke baad kaun sa gcc flag rukta hai (assembly emit karta hai)?
-S (.s produce karta hai)
Kaun sa gcc flag compile+assemble karta hai lekin link nahi karta?
-c (.o produce karta hai)
Preprocessor #include ke saath actually kya karta hai?
Poori header file ka text source mein physically paste kar deta hai.
Translation unit kya hai?
Preprocessor ka pure-C output (koi # directives nahi bache), jo compiler ka actual input hai.
SQ(x) ((x)*(x)) ko parenthesize kyun kiya gaya hai?
Macros text substitution hain; parens ke bina, operator precedence SQ(3+1) jaisi expressions tod deta hai.
Assembler kya produce karta hai aur woh runnable kyun nahi hota?
Ek object file (.o) machine code ke saath, lekin usmein unresolved symbols aur placeholder addresses hain.
Linker ka main kaam kya hai?
Object files + libraries combine karna, symbols resolve karna, aur addresses ko ek executable mein relocate karna.
undefined reference to 'x' kis stage par aata hai?
Linking par — ek symbol use kiya gaya lekin kabhi define nahi kiya gaya.
Static vs dynamic linking?
Static library code ko executable mein copy karta hai; dynamic ek reference store karta hai jo runtime par shared library se load hota hai.
Type-checking errors kis stage par hoti hain?
Compilation stage par (semantic analysis ke dauran).
Object file ke symbol table mein kya hota hai?
Woh symbols jo woh define karta hai aur woh symbols jo use chahiye (undefined references).
C program ke liye file extension chain?
.c → .i → .s → .o → executable

Concept Map

input to

produces .i

input to

produces .s

input to

produces .o

stitched by

plus libraries

handles

does

combines many

C source .c

Preprocessor

Translation unit

Compiler

Assembly

Assembler

Object bytes

Linker

Executable

Hash directives + macros

Lex parse type-check optimize