Show understanding of program libraries
5.1 Operating Systems – Program Libraries
What is a Program Library?
Think of a library like a real‑world book library 📚. Instead of books, it holds reusable pieces of code – functions, classes, and resources that many programs can use. This saves time and keeps code tidy.
Types of Libraries
- Static libraries (.a or .lib) – copied into the program at compile time.
- Dynamic / Shared libraries (.so, .dll, .dylib) – loaded at run‑time and shared by many programs.
- Header files (.h, .hpp) – contain declarations so the compiler knows what functions exist.
How Libraries Work (Static vs Dynamic)
| Step | Static Linking | Dynamic Linking |
|---|---|---|
| 1. Compile source code | Code + library code are merged into one executable. | Executable contains only references to library functions. |
| 2. Load at run‑time | All code already present – no extra loading. | Operating system loads the shared library into memory. |
| 3. Memory usage | Each program has its own copy – more memory. | One copy shared by all – saves memory. |
Linking Process Explained
- Compiler reads
.cfiles and.hheaders. - Assembler turns code into object files
.o. - Linker resolves symbols:
- Static: copies library object files into the final executable.
- Dynamic: records references to shared libraries.
- Result: an executable that can run on the target OS.
Benefits of Using Libraries
- ?? Reusability – write once, use many times.
- ?? Modularity – separate concerns into different files.
- ?? Maintainability – update library, all programs benefit.
- ?? Space optimisation – shared libraries reduce memory footprint.
Exam Tips
- Explain the difference between static and dynamic linking using a real‑world analogy.
- Show the linking steps in order – use a numbered list or table.
- Remember that
header filesonly contain declarations; the actual code lives in the library. - Use the symbol
+to show that static libraries are merged into the executable. - When asked about memory usage, compare
staticvsdynamicwith a simple diagram or table.
Revision
Log in to practice.
2 views
0 suggestions