Program libraries
| English | Chinese | Pinyin |
|---|---|---|
| program library | 程序库 | chéng xù kù |
| subroutines | 子程序 | zi chéng xù |
| library routines | 库例程 | kù lì chéng |
| static library | 静态库 | jìng tài kù |
| executable | 可执行文件 | kě zhí xíng wén jiàn |
| dynamic library | 动态库 | dòng tài kù |
Eleven lines that broke the internet
- In March 2016 a programmer deleted a tiny library called
left-padfrom a public repository. It was eleven lines long: it added spaces to the front of a string. - Within hours, builds failed at companies around the world. Thousands of programs had never written those eleven lines. They had all borrowed them, and the borrowing had been invisible until the moment it stopped working.
- That is how modern software is made. A program library 程序库 is the borrowing, done on purpose, and this lesson is what it gives a developer, what it costs, and the two ways it can be attached to a program.
What a program library is
- A program library is a collection of pre-written, tested routines, subroutines 子程序, classes and modules, that a program can use instead of writing its own.
- A maths library, a graphics library, a network library, a path-finding library, a compression library: each solves one area of problems once, for every program that needs it.
- Software under development is normally constructed from such library routines 库例程 plus the code that is specific to the new program.

The new code is the small part; the borrowed code is the large part
Computing concept lab
Classify concrete examples by the computing idea they demonstrate.
A program library is:
Libraries provide ready-made, tested code (maths, graphics, network…) that a program can call instead of writing its own.
A collection of ready-made, reusable, tested code is a program ______.
Libraries save time and reduce bugs.
The benefits to the developer
- The routines are already written and tested, so development is faster and cheaper.
- They are reliable: used by many programs, so largely error-free.
- The developer needs no expertise in that area, graphics, encryption, path-finding, and can spend the time on the program's own features.
- The program is easier to maintain, because common code lives in one place, and a team can work in parallel, each member using the same routines.
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.
Which are benefits to a developer of building software from library routines? Select all that apply.
Tested routines, no expertise needed, easier maintenance. The program's own code still has to be tested.
The drawbacks
- The routine may not fit the exact need, and the developer cannot change it, so they must work around its limits.
- The program depends on the library: a bug, a missing update or a withdrawn library, as with
left-pad, becomes the program's problem. - The library's licence may restrict how the program can be sold or shared, and unused routines can make the program larger.
Worked example: the restaurant robot
- A team writing the software for a restaurant robot uses a program library that includes a routine to find the shortest path between tables. Explain two benefits and one drawback to the team.
- Benefit: the routine is already written and tested, so the team saves time and can trust its results. Benefit: the team need not understand path-finding algorithms themselves and can spend the time on the robot's own features.
- Drawback: the routine may not handle the restaurant's exact needs, moving chairs, one-way aisles, and the team cannot alter it, so they may have to work around its limits.
- Tie each point to this team and this routine. A generic "saves time" earns less than "saves the time of writing and testing a path-finding algorithm".
Which is a drawback for the robot team of using the path-finding routine from a library?
The team cannot change library code, so any mismatch with their needs must be worked around. Not needing to understand the algorithm is a benefit, not a drawback.
Static libraries
- A static library 静态库 is copied into the executable 可执行文件 when the program is compiled and linked.
- The program then stands alone: it needs no other file to run.
- The cost: every program carries its own copy, so executables are larger, and a library update means rebuilding every program that uses it.
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.
Dynamic libraries and DLL files
- A dynamic library 动态库, a Dynamic Link Library (DLL) on Windows or a
.sofile on Linux, stays a separate file and is loaded into memory only when a program calls it, at run time. - Executables are smaller; several running programs share one copy in memory; and a DLL can be updated once, a bug fix or a new device, without recompiling the programs that use it.
- Memory is used only while the library is needed. The cost: the program will not run if the DLL is missing or the wrong version.

Copied in at compile time, or loaded and shared at run time
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.
Match each trait to a static or a dynamic library.
Static linking copies code into the executable (bigger, self-contained); dynamic linking loads a shared library at run time (smaller, updatable once for all).
Put the events of a program using a DLL routine in order.
Loading happens at run time, on the first call, and one copy serves every program.
Worked example: the benefits of DLL files
- A software developer uses DLL files. Describe the benefits. [4]
- The executable file is smaller, because the library code is not copied into it.
- One copy of the DLL in memory is shared by every running program that uses it, saving memory.
- The DLL can be updated to fix a bug or support a new device without recompiling or reinstalling the programs that call it.
- The library is loaded only when it is needed, so memory is used only while a routine is in use.
Choosing static or dynamic
- The program must run on a machine where nothing else can be installed, an embedded device or a single downloaded file: static, because the executable is self-contained.
- Many programs on one system share the same routines, or the routines change often: dynamic, because one shared copy is smaller and is updated once for all.
- Justify with the trade-off: self-contained but large and rebuilt to update, or small and shared but dependent on the file being present.
A program that must run from a single file on a device where nothing else can be installed should use a dynamic library.
A dynamic library is a separate file that must be present. A static library is copied into the executable, which is exactly what a self-contained program needs.
Marks that slip away
- A DLL is loaded at run time, not compile time. That single phrase is the mark.
- Benefits are to the developer: written and tested already, no expertise needed, easier maintenance. "The program runs faster" is not on the list.
- A library is code, not a tool. The IDE is the tool; the library is what the program calls.
- A drawback must be a drawback of using the library: cannot alter it, may not fit, dependency. "It costs money" is rarely the point.
You've got it
- a program library is pre-written, tested code that a program calls instead of writing its own
- benefits: faster and cheaper development, reliable routines, no expertise needed in that area, easier maintenance, teams work in parallel; drawbacks: may not fit, cannot be altered, dependency
- static: copied into the executable at compile time, self-contained but larger and rebuilt to update
- dynamic (DLL): a separate file loaded at run time, smaller, shared by many programs, updated once for all