Static Libraries in C

What are C libraries?

One of the tools that compilers supply C programmers with are libraries. A library file contains a collection of functions and declarations for use by other programs and programmers. How may they be used? A team of engineers calculating the tension of a bridge may develop their own library of mathematical functions to complete common tasks.

Libraries make life easier

Libraries come in one of two flavors: static and dynamic. In this post we will be focused on static libraries.

Static libraries date back to the creation of C itself. Static libraries house a collection of object (.o) files.

Object files are products of the compilation phase and comprised mostly of machine code, but also contains information that allows the linkers to see what functions and global objects are located within the program.

No dependencies

Object files in static libraries are linked into the program during the linking phase of compilation and the object files are not required during runtime — only the executable file is required in order for the program to run.

Normally libraries are indexed, so linking a program whose object files are ordered and stored in one location is much faster than linking a program whose object files are separate within a computer. Additionally, when using specific libraries for certain tasks, the compiler has fewer files to search for —further increasing efficiency.

Drawbacks of Static Libraries

For all of a static library’s efficiencies, it comes at a price. Because all object files within a static library are linked into the program during compilation, the resulting executables can get quite large. One can think of static library archives as similar to a zip file, but without the compression.

Secondly, if the library code is to be updated, a programmer must recompile their program into a new executable and every program that uses that particular library contains a copy in its executable.

How to Create Static libraries

Static libraries have the .a format. To create static libraries, we use a command called ar for archiver.

The first step is determining which functions we would like to include in our library and preparing them for the archiver — I like to place all the functions I like to use in a single directory. Next, we will compile all of our .c source files into .o object files by tagging on the -cflag:

$ gcc -c *.c

Once this command is entered, the corresponding .o files will be present in the directory. We are now ready to create our library file!

Next, we will take all of our .o files and package them into a single .a file.

For a full list of the capabilities of the arcommand, take a look at its man page ( man ar) — its able to create static libraries, modify object files in the static library, list the names of object files in the library, and more.

man page for ar

Let’s suppose we will call our static library libsuper.a. We now have our necessary .ofiles. We will run the command:

ar -rc libsuper.a *.o

The -r flag tells the arcommand to create a library if it doesn’t already exist and replaces older existing object files in the library.

The -c flag tells the arcommand to create the library if it doesn’t exist.

We’ve created our static C library!

Now it’s time to employ our shiny new library.

Before we do so, let’s look at the linking option in the (lengthy) man page for gcc ( man gcc).

We specify the library to the compiler with -lname where name is the filename of the library without the prefix lib. Earlier, we created the library libsuper.a . We also specify the location for our libraries, namely the current directory using the flag -L.

Our final command to compile our program (we’ll name it prog in this example) is:

gcc main.c -L -lsuper prog