What are static libraries?

Benjamin Keener
4 min readOct 14, 2019

One of the most useful tools in modern computing: libraries. What is a library? A library is a collection of useful functions and methods that can be easily shared and used. In this article, we’ll be focusing on libraries in C, but most modern languages take advantage of libraries.

Usefulness of Libraries

Let’s say you are writing code and, because you are such an awesome developer, you write a bunch of nonspecific functions to help you achieve your goal. Great! Now you move on to your next program and find that you could really use some of those functions you already made. That’s where libraries come in. Instead of copying and pasting all of your functions, you could create a library of your useful functions and implement it in all the programs you want!

How Do We Take Advantage Of This in C?

Good question. If you’re unfamiliar with the compilation process in C using gcc, please read my previous article before continuing. In this article, I’ll be talking about static libraries, though there is another type of library called a “shared” library.

Compiling a Static Library Using gcc, ar, and ranlib

In order to make a library in C for Linux, we need to make an archive file. An archive is a group of object files that have been (usually) organized for the computer. The first step in this process is to take our C files and compile them into object files.

Example directory containing C source files

To turn all of these source files to object files, we need to compile them. We can use the command gcc -c *.c. This command will take all files that end with .c and compile them, without linking (-c). Be aware, however, that we need a header file which includes the prototypes to each of our functions. In my case, it’s holberton.h.

Example directory with C source files and compiled object files

After compiling each file, we’re left with a bunch of .o files scattered everywhere. Our next step is to archive all of these files into one central file for easier use. We can do this using ar -rc libfunctions.a *.o. This will take all files ending with .o and archive them into a file named libfunctions.a.

Example directory with C source files and compiled object files and archive file

As we can see, we now have an archive file in the same directory, titled libfunctions.a. We can look at what’s contained in this archive by using the archive command (ar) with the -t flag.

List of all object files in the archive

This shows us all of the contained object files within our archive. Cool, so we’re done, right? Almost! To optimize our library, we should index all of our functions and variables (symbols) to make it easier for our programs to find them and cut down on runtime. To do this we need to use the ranlib command. This command will index our archive file.

Command to index archive library

Great! Now we have our own library to use all of our awesome functions! Just remember to take your header file along for the ride, as it needs to be referenced by any program you’re looking to use these functions in.

Speaking of which, how do we utilize a library in a program?

Compiling a Program Which Utilizes a Library

In order to have the compiler understand what we are referring to when we use a function from our library, we need to use some flags with our compiler gcc.

Our command to compile a file main.c and make an executable main with the library is gcc main.c -L. -lfunctions -o main. Let’s break this down. gcc main.c simply tells gcc that it is compiling the main.c file. -L. tells gcc that it can look for library files in the directory . (the current directory). -lfunctions tells gcc that a library called functions is a library required, it will look for a file that starts with lib, ends with .a, and contains the name in our -l flag. In this case it is looking for our libfunctions.a file.

After running this command, our program can be run while taking advantage of any library-defined functions, just make sure you keep that header file around.

Did You Find This Article Helpful?

Please, let me know! I’d love to hear how I can improve and what I did well. You can follow me on twitter @TheBenKeener.

--

--