Program libraries
Don't reinvent the wheel
- Most programs reuse pre-written code instead of writing everything from scratch.
- That code comes packaged in program libraries.
- They can be linked into your program in two different ways.
What a library is
- A program library is pre-written code (subroutines, classes, modules) that programs reuse — e.g. a maths, graphics or network library.
- Benefits: saves time (off-the-shelf code), reliable (well-tested and widely used), and standardised (consistent behaviour).
Practice
A program library is:
Libraries provide ready-made, tested code (maths, graphics, network…) that a program can call instead of writing its own.
Practice
A benefit of using a library is that the code is:
Library code is tested, widely used and standardised — reusing it saves time and improves reliability.
Static vs dynamic libraries
- A static library is copied into the executable at compile time: the program stands alone, but the file is larger and must be rebuilt to update the library.
- A dynamic library (DLL,
.so) is loaded at run time: executables are smaller, the library is shared by many programs, and updating it once fixes them all.
Practice
A static library differs from a dynamic library because it is:
A static library is built into the executable; a dynamic library is loaded at run time and shared between programs.
Practice
A dynamic library can be shared by many programs and updated once for all of them.
Because it is loaded at run time, one shared dynamic library serves many programs; updating it fixes them all without rebuilding each.
You've got it
Key idea
- a library = reusable pre-written code; benefits = time, reliability, standardisation
- static library: copied into the executable (stand-alone, larger, rebuild to update)
- dynamic library: loaded at run time (smaller, shared, updated once for all)