Program libraries · 程序库
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| program library/ˈprəʊɡræm ˈlaɪbrəri/ | 程序库 | chéng xù kù |
| subroutines/ˈsʌbruːtiːnz/ | 子程序 | zi chéng xù |
| library routines/ˈlaɪbrəri ruːˈtiːnz/ | 库例程 | kù lì chéng |
| static library/ˈstætɪk ˈlaɪbrəri/ | 静态库 | jìng tài kù |
| executable/ɪɡˈzekjʊtəbl/ | 可执行文件 | kě zhí xíng wén jiàn |
| dynamic library/daɪˈnæmɪk ˈlaɪbrəri/ | 动态库 | 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.
弄坏了互联网的十一行代码
- 2016 年 3 月,一名程序员从一个公共仓库删除了一个叫
left-pad的小库。它只有十一行:给字符串前面补空格。 - 几小时内,世界各地公司的构建纷纷失败。成千上万的程序从未自己写过那十一行。它们全都借用了它,而这种借用在它停止工作的那一刻之前一直是看不见的。
- 现代软件就是这样做出来的。程序库(program library)是有意为之的借用,这一课讲它给开发者什么、代价是什么,以及把它接到程序上的两种方式。
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
什么是程序库
- 程序库是一组预先写好、经过测试的例程——子程序(subroutines)、类和模块——程序可以使用它们而不必自己编写。
- 数学库、图形库、网络库、寻路库、压缩库:每个把一类问题解决一次,供每个需要的程序使用。
- 开发中的软件通常由这样的库例程(library routines)加上新程序特有的代码构建而成。

新代码是小的那部分;借来的代码是大的那部分
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.
缺点
- 例程可能不完全符合需求,而开发者无法修改它,只能绕开它的局限。
- 程序依赖这个库:一个 bug、一次缺失的更新或一个被撤回的库——就像
left-pad——都会变成程序的问题。 - 库的许可证可能限制程序怎样出售或分享,未使用的例程也可能让程序变大。
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.
静态库
- 静态库(static library)在程序编译和链接时被复制进可执行文件(executable)。
- 程序于是独立存在:运行时不需要其他文件。
- 代价:每个程序都带自己的一份副本,所以可执行文件更大;库更新意味着要重新构建每个使用它的程序。
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
动态库和 DLL 文件
- 动态库(dynamic library)——Windows 上的动态链接库(DLL)或 Linux 上的
.so文件——保持为单独的文件,只在程序调用它时才在运行时加载到内存。 - 可执行文件更小;几个运行中的程序在内存中共享一份副本;DLL 可以更新一次——修一个 bug 或支持新设备——而不必重新编译使用它的程序。
- 只在需要库时才占用内存。代价:DLL 缺失或版本不对时程序无法运行。

编译时复制进去,或运行时加载并共享
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. · 按顺序排列使用 DLL 例程的程序事件。
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.
例题:DLL 文件的好处
- 一名软件开发者使用 DLL 文件。描述其好处。[4]
- 可执行文件更小,因为库代码没有被复制进去。
- 内存中的一份 DLL 副本被每个使用它的运行中程序共享,节省内存。
- DLL 可以更新来修 bug 或支持新设备,而不必重新编译或重新安装调用它的程序。
- 库只在需要时加载,所以只在例程使用期间占用内存。
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.
容易丢掉的分
- DLL 在运行时加载,不是编译时。这一个短语就是那一分。
- 好处是对开发者的:已经写好并测试、不需要专长、更易维护。"程序运行更快"不在列表上。
- 库是代码,不是工具。IDE 是工具;库是程序调用的东西。
- 缺点必须是使用库的缺点:无法修改、可能不合适、依赖。"它要花钱"很少是重点。
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
你掌握了
- 程序库是程序调用而不必自己编写的预写、已测试代码
- 好处:开发更快更便宜、例程可靠、不需要该领域的专长、更易维护、团队并行工作;缺点:可能不合适、无法修改、依赖
- 静态:编译时复制进可执行文件,自成一体但更大,更新要重建
- 动态(DLL):运行时加载的单独文件,更小,被许多程序共享,一次更新惠及所有