EVERYTHING YOU ALWAYS WANTED TO KNOW ABOUT STATIC LIBRARIES BUT WERE AFRAID TO ASK.

(This article applies for UNIX systems only.)

What is a C static library?

A C library is just a set of named functions.

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. Static libraries are created from object files using a librarian utility of some kind. One such programs is ar (ar as in archive).

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.

Why using them?

One major advantage of static libraries being preferred even now is speed. There will be no dynamic querying of symbols in static libraries. Many production line software use static libraries even today.

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 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. Static 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 advantage 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 will be similar, or even smaller if the compiler eliminates the unused symbols.

How to create static libraries?

To create a static library, we need to specify to the compiler, which is GCC in our case, that we want to compile all library codes (*.c) into object files (*.o) without linking. To do that we are going to use the command below.

Note that the “*.c” matches all files in the current working directory with the “.c” extension.

For example, let us take two c files, add.c, and mul.c and a header file that contains the prototypes of these functions. The picture below shows the output generated after using the command.

Once we have object file(s), we can now bundle all object files into one static library.
To create a static library or to add additional object files to an existing static library, we have to use the ar (archiver) program. We can use a command like this:

This command creates a static library named “libname.a” and puts copies of the object files “add.o” and “mul.o” in it. The ‘c’ flag tells ar to create the library if it doesn’t already exist. The ‘r’ flag tells it to insert object files or replace existing object files in the library, with the new object files.

In order to actually use the newly created static library we can use the command below to create our final executable program:

When using GCC we must use the following flags to create the static library:

  • -L : specifies the path to the library .We can use -L in order to point to the current directory and -L/home/tmp to point to the /home/tmp directory.
  • -l : adds the library’s name to the list of object file names given to the linker.

This will create a program using the object file “main.o”, and any symbols it requires from the “name” static library.

After a library archive is created, or modified, there is a need to index it. This index is later used by the compiler to speed up symbol-lookup inside the library, and to make sure that the order of the symbols in the library won’t matter during compilation. The command used to create or update the index is called 'ranlib'.

Cheers!