OCR H446, section 1.2.2 (f)
A real program is rarely a single file of code. It is built from several parts that are written, translated and then put together. Libraries, linkers and loaders are the tools involved.
Libraries
A library is a collection of ready-made, tested routines (subroutines, functions and classes) that programmers can use in their own programs instead of writing the code again.
- Examples: maths functions such as square root and sine, graphics routines for drawing, networking code, encryption, and file handling.
- The routines are pre-written and well tested, so using them saves time, reduces errors, and gives access to expert code (such as secure encryption) that would be hard to write.
- Programs use a library by calling its routines, and the library must then be joined to the program.
Drawbacks of libraries:
- The compiled program can get much bigger, because a library holds many routines that the program doesn't use (when it is statically linked).
- The code wasn't written by the programmer, so it brings uncertainty: it may need further testing, and it takes time to learn how to use it.
- The program depends on the library: a bug or security flaw in it, or the library being changed or withdrawn, affects the program too (2.1.2 (d)).
The linker
When a compiler translates source code, it produces an object file for each source file. Calls to routines in other files or libraries can't be fully resolved yet, because the compiler doesn't know where they are.
A linker is a utility that combines the separate object files and the library routines into one executable program. Its job is to:
- Join the object modules into a single file.
- Resolve references: work out the addresses of routines and variables that are used across files, so that each call goes to the right place.
- Include the needed routines from the libraries.
Static and dynamic linking
| Static linking | Dynamic linking | |
|---|---|---|
| When | The library code is copied into the executable when it is built | The library stays separate and is joined when the program runs |
| Size of program | Larger | Smaller |
| Library file needed at run time? | No: everything is inside | Yes: the library must be installed |
| Updating the library | The program must be rebuilt | The library can be updated without changing the program |
| Sharing | Each program has its own copy | Many programs share one copy in memory |
| Example | A stand-alone program | DLL (dynamic link library) files on Windows |
Dynamic linking saves disk space and memory, and means that a fix to a library helps every program that uses it. The risk is that the library may be missing or the wrong version.
The loader
A loader is part of the operating system. When you run a program, the loader:
- copies the executable from the disk into main memory;
- sets up the memory the program will use (for its code, data and stack);
- adjusts (relocates) addresses in the program if it is loaded at a different place than expected;
- loads the dynamic libraries that the program needs, and links them;
- starts the program by setting the program counter to its first instruction.
The whole sequence
Why use libraries, linkers and loaders?
- Reusing code saves effort, and reduces faults, as the code is tested.
- Large programs can be split into modules, worked on by different programmers, and compiled separately: only the changed file needs to be recompiled.
- Dynamic libraries save space and make updates easy.
- The loader makes it possible for several programs to be in memory at once, at different addresses.
Worked example
Task: a game uses a graphics library. Explain the roles of the compiler, linker and loader, and the difference between static and dynamic linking for the library.
- The compiler translates each of the game's source files into object code. Calls to the graphics routines are left unresolved.
- The linker combines the object files and resolves the calls to the graphics library, producing the executable.
- If the library is statically linked, its code is copied into the game's file, so the file is bigger, but the game runs on any computer. If it is dynamically linked, the game stays smaller and loads the library's DLL at run time, so the library must be installed, but several games can share it, and a library update improves them all.
- When the player starts the game, the loader copies it (and the DLL) into RAM, fixes addresses, and starts it running.
Exam tip: keep the three jobs apart: a library is a set of pre-written routines, the linker joins object code and libraries into an executable (resolving addresses), and the loader puts the executable into memory and starts it. Know static against dynamic linking.
Check yourself
What is a library?
A collection of pre-written, tested routines that programmers can use in their own programs instead of writing the code again.
What does a linker do?
It combines separate object files and library routines into a single executable program, resolving the addresses of routines used across files.
What does a loader do?
It copies the executable from storage into main memory, sets up its memory and addresses, loads any dynamic libraries it needs, and starts it running.
Give one advantage of dynamic linking over static linking
The executable is smaller, many programs can share one copy of the library, and the library can be updated without rebuilding the programs.