A library is essentially a file that contains compiled object modules (a module is the object file produced from compiling a single source file). A program can call, or import, routines and access data defined in another library. Such libraries can contain numeric analysis functions, multimedia packages, graphics, etc. There are two main categories of libraries: static and dynamic. When you link your code with a static library, the program copies the code and data it needs from that library into the executable file. Thus, a statically linked program that uses the printf() function contains a copy of this function in its executable file. By contrast, a dynamic library (or runtime library) is linked to a program at runtime: references to functions and data from the runtime library are resolved at runtime and are not copied into the program’s executable file.
Most C and C++ implementations provide a runtime library which contains the standard functions and data structures of the language (e.g., the printf() function, operator new and delete, iostream objects, etc.). A runtime library is usually shared?all processes and applications on the same machine share a single copy instead of having multiple copies of the same library code. A runtime library offers three advantages compared to a statically linked one:
- It reduces the program’s size because the library’s code is not included in the program’s executable file
- Changes made to the runtime library (e.g., an upgrade, bug fixes) don’t require that the programs be relinked; the next time you run the program, it automatically loads the new library version and accesses its code and data.
- It saves considerable amount of disk space because its code is shared rather than being copied into each program file.
That said, dynamic libraries can also cause serious difficulties if several programs depend on the same library, and the library is changed (this problem is know as the “DLL hell” in windows, although other platforms suffer from this problem, too). Additionally, the runtime overhead of dynamic linking is considerable and slows down execution speed.