Static Library

In computer science, a static library or statically-linked library is a set of routines, external functions, and variables which are resolved in a caller (user source code) at compile-time and copied into a target application by a compiler, linker, or binder, producing an object file and/or a stand-alone executable program. The executable and the process of compiling it are both known as a static build of the program. Historically, libraries could only be static but modern operating systems are allow for dynamic linking at runtime. Static libraries are either merged with other static libraries and object files during building/linking to for a single executable or loaded at run-time into the address space of their corresponding executable at a static memory offset that is determined at compile-time/link-time.

Advantages and Disadvantages

There are several advantages to statically linking libraries with an executable instead of dynamically linking them. The most significant advantage is that the application can be certain that all of its libraries are present and that they are the correct version. This avoids dependency problems, known colloquially as DLL HELL or more generally dependency hell. Statical linking can also allow the application to be contained in a single executable file, simplifying distribution and installation.

With static linking, it is enough to include those parts of the library that are directly and indirectly referenced by the target executable (or target library). With dynamic libraries, the entire library is loaded, as it is not known in advance which functions will be invoked by applications. Whether this advantages is significant in practice depends on the structure of the library.

In static linking, the size of the executable becomes greater than in dynamic linking, as the library code is stored within the executable rather than in separate files. But if library files are counted as part of the application then the total size wil be similar, or even smaller if the compiler eliminates the unused symbols.

Environment Specific

On Microsoft Windows, it is common to include the library files an application needs with the application. On Unix-like systems this is less common as package management systems can be used to ensure the correct library files are available. This allows the library files to be shared between many applications leading to space savings. It also allows the library te be upgraded to fix bugs and security flaws without updating the applications that use the library. In practice, many executables (especially those targeting Microsoft Windows) use both stat and dynamic libraries.

Linking and Loading

Any static library function can call a function or procedure in another static library. The linker and loader handle this the same way as for kinds of other objects files. Static library files may be linked at run time by a linking loader (e.g., the x11 module loader). However, whether such a process can be called static linking is controversial.

Creating Static Libraries in C/C++

Static libraries can be easily created in C or C++. These two languages provide storage-class specifiers for indicating external or internal linkage, in addition to providing other features. To create such a library, the exported functions/procedures and other objects variables must be specified for external linkage (i.e., by not using the C ‘static’ keyword). Static libraries filenames usually have “.a” extension on Unix-like systems and “.lib” extension on Microsoft Windows.

For example, to create an archive named “libclass.a” from the files “class1.o”, “class2.o”, and “class3.o”, the following command would be used:

ar rcs libclass.a class1.o class2.o class3.o

To compile a program that depends on class1.o, class2.o, and/or class3.o, use the following:

cc main.c libclass.a

Or (if libclass.a is placed in the standard library path </usr/local/lib>)

cc main.c -lclass

Or (during linking)

ld main.o -lclass
or
cc main.o -lclass

instead of having to do this:

cc main.c class1.o class2.o class3.o