A Beginner’s Guide to Static Libraries in C

Photo by Jessica Ruscello on Unsplash

You’ve done it: you worked tirelessly on a series of functions to plug into your main file, but now you’re typing a rambling stream of files into your command line to compile them together. There has to be a better way, right?

(Enter libraries.)

A library is a single file that contains any object files that are needed for a project. During compiling, the linker uses a library to access several object files as one item.

There are two different types of libraries in a Unix system. A static library (also called an archive) is an indexed library that is linked to the program once during the linking phase of compiling. A dynamic library (also called a shared library) is referenced twice during compiling. Rather than linking the full library to the program, the linker simply checks that all necessary object files are present in the library. Then, the dynamic loader loads the needed shared files into memory before running the executable.

Each type of library has its own benefits. The older of the two, static libraries take less time to compile and do not require a library owner to share its source code with those who want to use the library.

Creating a static library

In order to create a static library, you must first prepare all of your files. First, ensure that you have a header file, complete with header guards and any needed prototypes.

Next, compile your c files into object files.

$ gcc -c letsmakeanobjectfile.c

The -c flag instructs the compiler to take the c file through the preprocessor, compiler, and assembler but not the linker.

After you compile your object files, you’re ready to create your library! To do so, you can use the archiver command:

$ ar rc newlibrary.a letsmakeanobjectfile.o moreobjectfiles.o

The r flag instructs the archiver to replace old versions of object files in the library with new versions. The c flag tells the archiver to create a new library if it does not already exist.

After creating your library, you will still need to index it.

$ ranlib newlibrary.a

Indexing a library organizes the file’s symbols, making it much faster for the compiler to reference the symbols in the library. This process will need to be repeated any time that the library is updated.

Using a static library

When compiling a program with another function, you may have used a command like this:

$ gcc main.c function.c -o executablename

When compiling with a library, the process is similar. Instead of entering in a function, use the -L. flag to let the linker where to look for the library. You will also begin the name of your library with -l to tell the linker that it is the library to reference.

$ gcc main.c -L. -lnewlibrary.a -o executablename

Since the library is not needed until the linker stage of the compiler, all commands related to the library must come after the file name.

Now, you can use your executable!

By creating a static library, you are able to speed up your compilation time, put all of your object files in one place, and save yourself the hassle of typing in every file needed each time that you compile a program.

Software engineering student and lover of mountains.