Dynamic and static libraries in C

Shelves that look like boxes hold vintage books upon a wooden wall.
Photo by Paul Melki on Unsplash

When working on large projects, it can be frustrating to compile many files together. This is even more of an issue when sharing the files with others. Libraries enable us to organize and use functions without having to copy the source code into each location.

If you’ve done any programming in C, you’ve likely begun your file with something like

When you do this, you are calling the header function for the standard library. Each of the files in the standard library exist as source code, but you don’t have to compile them into your file because the compiler already has access to the library. You can create your own library to function similarly.

There are two different types of libraries, static and dynamic. A static library, also known as an archive, is an indexed library that the compiler references only once. The linker copies the code of the library when making an executable. Static libraries take up more memory, but they can speed up run time.

A dynamic library, also known as a shared library, links the code while running the executable, not during compilation. Instead of copying the code, the compiler simply checks that all object files are present during the linking stage. The dynamic loader then loads the shared files into memory before running the executable. Dynamic libraries are beneficial because they do not require a user to recompile the main file every time there is an update in the library. However, they can hurt the speed of an executable.

Creating libraries

In order to create a library, first create a header file containing all prototypes that you intend to include. Then, you’ll need to compile your code.

The -c flag instructs the compiler to take the files through every compilation stage except for the linker. If you are creating a dynamic library, you will also need to use the -fPIC flag. This creates position-independent code, meaning that it will execute correctly no matter where it is in memory.

Creating static libraries

To create a static library, use the archiver command:

The r flag instructs the archiver to replace old versions of files with new ones while the c flag instructs it to create a new archive if one does not already exist. All static libraries end in .a for archive.

Static libraries must be indexed after creation. This organizes the library’s symbols, making it easier and faster to access them.

Creating Dynamic Libraries

The -shared flag creates the shared library. All dynamic libraries end in .so for shared object.

Let’s use the ldd command to print our shared object dependencies.

Finally, export the library path so that the child processes can access it using the environmental variable.

Learn more about child processes in this article, where I break down the shell.

When we check this again using ldd, we can see that the shared dependencies have been updated.

Once it is created, you can use the nm command to list symbols in your library.

Using libraries on a Linux system

In the words of Arthur, “Having fun isn’t hard when you’ve got a library card!”

Now that you’ve created your library, it’s time to use it. No matter which type of library you’re using, there are a few things to keep in mind. First, since the compiler will not use the library until the linker stage, all commands related to the library must come after the file. Second, the -l flag will give the compiler the name of the library to reference.

Using a static library

Static libraries are compiled along with the files needed for a program.

When working with static libraries, the -L. flag tells the compiler where to look for the library.

Using a dynamic library

In dynamic libraries, there are no additional flags needed. You can use the library by compiling it with your files.

Because we already updated the path for the library, we do not need to specify anything using -L.

But don’t forget, dynamic libraries are also called shared libraries. So what happens if we want other people to be able to use them? First, place the library in a common area and update the permissions. Next, use the ldconfig command to update the cache and create a link to the shared library.

Once you have the hang of these steps, you’re ready to create your own libraries. Decorate a bookmark with the time you save on your next project!

Software engineering student and lover of mountains.