A C library is a set of named functions, for example dbinit()
or SQLConnect()
. Or, for that matter, fopen(3)
[34].
Libraries come in two flavors: static and dynamic.
Static libraries (also known as archives) have been around as long as C itself. Like a .zip
file, they're just a bag of object files — containing functions, of course — with a table of contents in front giving the address of each name[35]. Static libraries are created from object files using a librarian utility of some kind. One such programs is ar, for archive.
Static libraries are part of the build environment. Functions in static libraries are joined to a program's main module by a static linker at build time to produce an executable program. The executable incorporates the libraries' object code into its own body, making it completely self-sufficient.
Dynamic libraries are the new kid on the block, as these things go, arriving on the Unix scene circa 1985. Like a static library, a dynamic library is a collection of functions with a table of contents. They are referenced at build time to give the executable information about how they will eventually be used, but they aren't used until run time.
Dynamic libraries are part of the run-time environment. When a program is run, the run-time linker finds the dynamic libraries needed by the program, finds the addresses of the required functions, and assembles a runable image in memory. Missing libraries and/or missing functions — or the wrong versions of them — can lead to head-scratching and other amusing behavior.
In Windows® dynamic libraries are called dynamic link libraries (DLLs). In Unix they're normally called shared objects. But they're roughly the same thing.
What about .h files? | |
---|---|
C header files include functional prototypes, declarations (not definitions) of functions. Functional prototypes describe to the compiler each function's parameters, allowing the compiler to confirm that the function is being called correctly. Most of the functions declared in header files are implemented in libraries. However, there's no mechanical or automatic relationship between the functional prototypes in the header files and their implementation in a library. The For example, imagine a function
These errors are possible because C functions are identified to the linker by name only. On the upside, that makes the tools simple and easy to implement and, by the same token, simplifies the use of C libraries by other languages. The downside is that the work of ensuring that the right libraries are used becomes an administrative task instead of a technical one. |