5.3.3 · HinglishBuild Systems & Toolchain

Static libraries — .a - .lib, creation and linking

1,987 words9 min readRead in English

5.3.3 · Coding › Build Systems & Toolchain


WHY — static libraries kyun exist karti hain?

Do competing goals jo ek static library balance karti hai:

  • Reuse — code ek baar likho, kahin bhi link karo.
  • Selectivity — apne executable ko un functions se bloat mat karo jo tum kabhi call hi nahi karte.

WHAT — actually ek .a / .lib kya hota hai?


HOW — ek library kaise banate hain (first principles)

Ek library teen conceptual stages mein banti hai. Aao command sequence derive karein.

Humhe machine code chahiye lekin koi finished program nahi, isliye hum compiler ko linking se pehle -c ke saath rok dete hain.

gcc -c add.c    -o add.o     # Why -c? compile only, produce relocatable .o
gcc -c sub.c    -o sub.o     # no main() needed; libraries have no entry point

Stage 2 — .o files ko ek .a mein archive karo

ar rcs libmath.a add.o sub.o

Har flag decode karna (yahi reason hai ki command aise dikhti hai):

flag matlab kyun chahiye
r members ko replace/insert karo .os add karo, purani versions overwrite karke
c quietly create karo archive exist nahi karta toh banao, koi warning nahi
s ek symbol index likho taaki linker symbols fast dhoond sake (warna ranlib chalao)
gcc main.o -L. -lmath -o app
  • -L. — current directory ko library search path mein add karo.
  • -lmathlibmath.a dhoondo (lib prefix aur .a suffix tumhare liye add ho jaate hain).

Figure — Static libraries — .a  -  .lib, creation and linking

HOW — linking actually code select kaise karta hai (deep part)

Yahi famous link order rule ka source hai.


Worked Examples


Common Mistakes (Steel-manned)


Active Recall

Recall Static lib build aur use karne ka minimal 3-stage pipeline kya hai?
  1. gcc -c *.c → object files. 2. ar rcs libX.a *.o → archive. 3. gcc main.o -L. -lX -o app → link, library apne user ke baad.
Recall

.a delete karne se executable kyun nahi toot-ta? Kyunki static linking ne build time pe zaroorat ka machine code executable mein copy kar diya tha; archive sirf ek build-time source tha.

Recall

ar rcs mein s tumhe kya deta hai? Ek symbol index taaki linker har member scan kiye bina symbols resolve kar sake.

Recall (Feynman, ek 12-saal ke bacche ko samjhao)

Socho ek LEGO bricks ka dabba (.a). Tumhare toy ke instructions (main.o) kehte hain "mujhe ek red 2×4 brick chahiye." Builder (linker) dabba kholega, sirf wahi red 2×4 nikalega jo tumne manga, toy pe lagaega, aur dabba band kar dega. Ek baar toy ban gaya, tum dabba phek sakte ho — brick already glued on hai. Lekin builder sirf wahi bricks uthata hai jo tum pehle se maang chuke ho, isliye dabbe tak pahunche se pehle maangna padega, baad mein nahi.



Flashcards

Unix vs Windows pe static library ka file extension kya hota hai?
lib<name>.a (Unix) aur <name>.lib (Windows/MSVC).
Unix pe .a archive kaunsa tool banata hai?
ar (jaise ar rcs libX.a *.o).
ar rcs mein flags decode karo.
r = members insert/replace karo, c = quietly create karo, s = symbol index likho.
Library sources compile karte waqt -c kyun use karna padta hai?
Relocatable object files pe rokne ke liye (no linking, koi main entry point required nahi).
Unix link order rule batao.
Ek library unke objects/libraries ke BAAD aani chahiye jo iske symbols reference karti hain (single-pass left-to-right resolution).
gcc -lmath main.o fail kyun hota hai lekin gcc main.o -lmath kaam karta hai?
Linker sirf wahi archive members pull karta hai jo currently-undefined symbols satisfy karti hain; lib pehle hone pe kuch bhi undefined nahi hota isliye kuch liya hi nahi jaata.
Kya runtime pe static .a present honi chahiye?
Nahi — iska code link time pe executable mein copy ho jaata hai; yeh sirf ek build-time dependency hai.
nm mein T vs U ka kya matlab hai?
T = symbol defined hai (text/code mein), U = symbol undefined hai (kahin aur se chahiye).
"Archive has no index" ko kya fix karta hai?
ar rcs se re-archive karo ya ranlib libX.a chalao.
Search karte waqt -lfoo kismein expand hota hai?
-L search paths mein libfoo.a (ya libfoo.so) naam ki file mein.
GNU ld mein do static libs ke beech cyclic dependencies kaise handle karte ho?
Unhe wrap karo: -Wl,--start-group -lA -lB -Wl,--end-group.

Connections

Concept Map

gcc -c compile only

addresses are placeholders

ar rcs archive

s flag writes

maps symbol to member

-L path plus -lname

copies only used code

resolves relocations

solves

contrast with

bound at run time

.a no longer needed

C source files

Relocatable .o objects

Relocations to fill later

Static library .a / .lib

Symbol index

Linker

Final executable

Reuse plus selectivity

Shared library .so / .dll

Run-time binding