Reasons to use “C” Static Libraries

In my previous post, we reviewed the four steps of the compilation process. During the fourth step of linking, in order to produce an executable program existing pieces of code have to be rearranged and the missing ones filled in. As you may recall, during this stage the linker arranges the pieces of object code so that functions in some pieces can successfully call functions in other pieces. It also adds pieces containing the instructions for library functions used by the program.

There are two types of libraries: static and shared. For today’s article, I will discuss why and how to use a static library.
A collection of encyclopedias, also known as society’s version of a static library

There are several advantages to statically linking a library with an executable.

  1. The most significant 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 dependency hell.
  2. Static linking can also allow the application to be contained in a single executable file, simplifying distribution and installation.
  3. You only need to include those parts of the library that are directly and indirectly referenced by the target executable. With dynamic libraries, the entire library is loaded, as it is not known in advance which functions will be invoked by applications.

Now that you have three solid reasons to use a static library, let’s walk through the steps of making one of your very own.

Here is a directory of functions

We first begin with a directory of functions. Remember, a library is simply a collection of object files. You can recall from the previous article that object code is generated during the assembly step of compilation. The output of this step can be generated by passing the -c option during compilation.

Screenshot of gcc -c *.c and resulting object code files in directory

With our object code files now created, we can proceed with the creation of a static library. We will be typing the following into terminal.

ar -rc yourlibraryname.a *.o 
ranlib yourlibraryname.a
ar -t yourlibraryname.a

Before I proceed, let’s break down this code:

  • ar this is the command for archiver. For more information run man ar in your terminal.
  • -rc these are actually two flags: r tells ar to replace older object files with new files and c tells ar to create library if it does not exist
  • yourlibraryname.a you can name your library anything you want as long as you make sure to end the name with the .a extension
  • *.o the * wildcard tells the shell to include any files in the current directory ending with the extension .o into yourlibraryname.a

New or modified archives must be indexed! To do this we use another command called ranlib.

To go back and check the contents of your library, you can run ar with the -tflag.

Let’s see this all in action.

Give yourself a pat on the back because you’ve created your first static library! Now to use it, simply add the library’s name during compilation using the following flags:

gcc main.c -L. -lyourlibraryname 

Let’s look closer at this syntax:

  • -L tells linker that libraries might be found in the given directory, in this case we use . to tell compiler to look in the current directory
  • -l is used along with the library name — note how the .a extension is omitted here!

Like what you read? Give Elaine Yeung a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.