Much Ado About (Static) Libraries

Brett P Davis
3 min readFeb 28, 2020

In programming languages such as C, a static library is a group of files which contain all symbols required by the main function to operate, such as function and variables, which has been precompiled. Typically, a library will include two sections:
1: The header file, which contains all the function prototypes. This part essentially serves as a way to explain what the library contains, and how to use the functions it contains.
2: The pre-compiled object files for all the functions listed in the header file. This portion is where the actual functionality of the library is stored.

Considering the extra effort needed to make a library when simply compiling or including the files is an option, you may be wondering what makes libraries useful enough to bother with. Using a static library means that only the library/libraries being used need to be added during the linking stage of compilation, as opposed to one object file for each function. In smaller projects this difference is minimal, but the larger the project the more essential this becomes. In addition, static libraries are actually copied into the executable, as opposed to dynamically-linked libraries, which are linked at runtime. This has the primary benefit of avoiding any dependency problems, as you know for a fact everything needed is contained within the executable, as well as an increased start speed at runtime. This does, however, have the downside of increased file size if unnecessary symbols are present. There is also the major downside of needing to recompile all dependent programs any time a change is made to the library.

The reason static libraries are able to perform these tasks is because in the last stage of compilation, linking, the pre-compiled library is copied into the executable, ensuring that all functions that are defined in the library and called in the program can be accessed with only the single executable distributed.

Static libraries are an archive of object files, and as such are created using the ar or archive command. ar is a GNU program that is used to create, modify, and extract from archives. To create a static library, the following command is used:
ar rcs [libraryname].a [objectfile].o
r
is used to tell ar to replace any pre-existing files in the archive with the same name, allowing the library to be updated. c is used to tell ar to create the library if it does not already exist. s is used to write an index of the object files into the archive, even if there aren’t any actual changes.

Example of how to compile multiple .o files into a static library

In the example above, multiple .o files are stored in a folder, and are all compiled at once into a single library my_library.a.

Proof library was archived correctly
Confirmation library was correctly compiled

Now that we have our library compiled into a useable format, let's test it by making a function dependant on one of its files. the _puts function, stored in 3-puts.o, can be used to print to stdout.

Now instead of having to link any of the files contained in the library, we can instead just reference the library.

Ultimately, static libraries like many things have benefits and downsides and you must evaluate if it fits your needs before deciding what to use for any given project.

--

--