Creating and Using Static Libraries (C)

Afa Madza
3 min readFeb 12, 2018

--

Disclaimer: C static libraries do not actually look like this.

A statically-linked library, or static library for short, is essentially a file that contains several .o object files that can be used as a single entity during the linking phase of a program. If you are not familiar with how a program is compiled, please feel free to read my post on it here. Usually, the library is indexed so that the system is able to retrieve symbols such as functions and variables faster. During the linking phase, the linker will search the library for .o files that provide any of the missing symbols in the main program. As a result, linking a program with its object files ordered in a library is faster than if the object files were located elsewhere on the disk because the system has fewer files to look for. In this article, I’ll explain how to create and use a static library to facilitate writing code that you can manage efficiently.

How to Create a Static Library

The command to create a static library is ar, which means “archiver.” The ar command doesn’t just create static libraries (which are basically archive files). It can also list the names of object files in the library and modify .o files contained in the static library. If you’ve ever used a .zip or a .tar file, you know how easy it is to create a file that contains other files. That is exactly what a .a archive file is: a file containing other files. To create object files, simply type and execute gcc -c *.c. This command tells the compiler to generate object files for all the .c files in the current directory. In order to create a static library, we can use the following command:

ar rc libholberton.a *.o

In this command, the r flag ensures that older files will be updated by being replaced with new object files. The c flag means that the library will be created if it does not already exist. Finally, the *.o is a wildcard operation to include all files ending with .o in the static library. It is important to note the format for naming a .a file:

lib<your_lib_name>.a

At this point, you can store your archive in a library directory. For instance, some coders like to store archives in a standard directory once they are sure that it works. On Linux and Mac the usr/local/lib directory is a good choice because that is the directory set aside for the user’s custom libraries.

Using Static Libraries

The point of creating a static library is to use it with other programs. If your library is in a standard directory, then you can compile your code as follows:

gcc test_code.c -lholberton -o test_code

In the above command it is worth noting that your source code, test_code.c in this case, needs to be listed before the -l flag. The expression, -l combined with holberton tells the compiler to look for an archive called libholberton.a. This is why it is important to use the standard format for naming that I described earlier. For instance if test_code.c was the following:

#include "holberton.h"int main(void)
{
_puts("Hello World!");
return (0);
}

Typing and executing gcc test_code.c -lholberton -o test_code would generate an executable file called test_code. In order to accomplish this, the compiler looks through the library that is specified with the -l flag for the _puts function object code. Executing test_code like so: ./test_code would give us the following output: Hello World!. Now that you know how to create and use static libraries, I hope you have fun coding!

--

--

Afa Madza

West Point grad turned aspiring Software Engineer. USMA ’16 | Holberton School Batch 5