Build Systems, Libraries & Dependencies

a static library (.a archive)

A static library is the simpler of the two kinds of library: a bundle of compiled code that gets copied directly into your program when you build it, so the finished executable carries its own copy and needs nothing extra to run. Think of photocopying the pages you need from a reference book and stapling them into your own report — once done, you can throw the original book away and your report is complete.

Physically, a static library on Unix is an archive file, usually named like libfoo.a, which is little more than a bundle of object files (.o files) packaged together, often with the ar tool. When you link your program against it, the linker reaches into the archive, pulls out exactly the object files that hold functions your program actually uses, and copies their machine code into your final executable. After that the .a file is no longer needed at run time; the code is baked in.

Why it matters: static linking makes a self-contained executable that you can copy to another machine and run with no missing-library worries, which is great for simple deployment. The trade-offs are size and updates: every program that statically links a library carries its own copy of that code (wasting disk and memory if many do), and if a bug is fixed in the library, you must rebuild every program to pick up the fix — the old copy stays frozen inside each binary.

Make a static library and link it in: gcc -c util.c -o util.o ar rcs libutil.a util.o # bundle .o files into an archive gcc main.c -L. -lutil # copy needed code into the binary The resulting program runs even after libutil.a is deleted.

A .a archive is a bundle of object files; the linker copies the needed ones into the executable.

Static linking copies the code in at build time, so a later fix to the library does not reach already-built programs — they keep the old code frozen inside until you relink them.

Also called
archivestatic link library封存檔靜態連結函式庫