Static Libraries in C

What is a Static Library and why use one?

In C, a library is a list (or archive) of object files for functions you wish to reference in one place. Libraries are extremely helpful when compiling and writing programs.

If you have a program that calls multiple functions without a library, during compilation the Linker will need to link the called functions’ object files separately, which could take some time as the called functions’ object files may not be indexed nicely in memory. If you create a library for these called functions, the Linker will only need to reference the single library to link the called functions’ object code to the executable code of your program.

Libraries are also helpful when writing programs because you can reference a previously-created function instead of creating it again.

How do Static Libraries work?

As previously mentioned, Static Libraries are used when compiling a program.

The program you wish to compile will go through the compiling steps through Assembly, which will provide the object code of the program to the Linker. The linker will then look in the library referenced in the program (our #include lines at the top of our source code) for the specific functions that are called in the program. When the object code for the called function is found, the linker adds the code to the executable file it will output. Once all the called functions are found and linked, the Linker produces the executable file, including the object code from the called functions.

How do you create a Static Library?

To create a Static Library, you use the command ar. Before we can use this command, however, we must make sure the functions we wish to add to the library are object files.

Here is a list of files int my current directory. I wish to create a library with the files end with .c. The file ending in .h is my header file listing the prototypes of the .c files.

To get the object files for these .c files, we’ll need to compile them. To do this, I will run gcc -c *.c . The option -c tells the gcc compiler to stop the compiling process to stop after the Assembly portion and output the object files. *.c tells gcc to compile all files in the current directory with the suffix .c.

After compiling the .c files, you can now see there are also the complementary .o object files.

Now that we have the proper object files, we can use the ar command to create the library libtest.a as such:

r is an option to replace older object files with newer ones (if applicable); the c option tells the ar command to create a library if it doesn’t exist. *.o specifies to create the library using all the files with suffix .o (the object files). If you did not want all object files, you could specify them separately, such as: ar rc libtest.a _print_rev.o _strlen.o _putchar.o

Now, when we list the files in the current directory, we can see our newly created library! Before we can start using it however, we need to create our index by using the command ranlib. The index will act as a sort of table of contents for the library.

Note: if you ever need to update the index of the library, run ranlib again for the library.

How do you use a Static Library?

Now that we have a library, how do we use it?

  1. We must be sure the library we are using is included in our program:
    Notice: we will be #including the library <stdio.h> in main.c so we may use the printf function.
  1. When compiling, we must include the library:
    Notice:
    A. -L. tells gcc to look for the library in the current directory.
    B. When we reference the library, we remove lib and .a, but add -l
    C. -o maintest tells gcc to name the executable file maintest

Now you have a program that is ready to run!


Did you know there are also libraries in C known as “dynamic,” or “shared”?
We may be looking into this type of library in the future.

Like what you read? Give Kristen Loyd a round of applause.

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