Reasons to use “C” Static Libraries
In my previous post, we reviewed the four steps of the compilation process. During the fourth step of linking, in order to produce an executable program existing pieces of code have to be rearranged and the missing ones filled in. As you may recall, during this stage the linker arranges the pieces of object code so that functions in some pieces can successfully call functions in other pieces. It also adds pieces containing the instructions for library functions used by the program.
There are two types of libraries: static and shared. For today’s article, I will discuss why and how to use a static library.
There are several advantages to statically linking a library with an executable.
- The most significant is that the application can be certain that all its libraries are present and that they are the correct version. This avoids dependency problems, known colloquially as dependency hell.
- Static linking can also allow the application to be contained in a single executable file, simplifying distribution and installation.
- You only need to include those parts of the library that are directly and indirectly referenced by the target executable. With dynamic libraries, the entire library is loaded, as it is not known in advance which functions will be invoked by applications.
Now that you have three solid reasons to use a static library, let’s walk through the steps of making one of your very own.
We first begin with a directory of functions. Remember, a library is simply a collection of object files. You can recall from the previous article that object code is generated during the assembly step of compilation. The output of this step can be generated by passing the
-c option during compilation.
With our object code files now created, we can proceed with the creation of a static library. We will be typing the following into terminal.
ar -rc yourlibraryname.a *.o
Before I proceed, let’s break down this code:
arthis is the command for archiver. For more information run
man arin your terminal.
-rcthese are actually two flags:
arto replace older object files with new files and
arto create library if it does not exist
yourlibraryname.ayou can name your library anything you want as long as you make sure to end the name with the
*wildcard tells the shell to include any files in the current directory ending with the extension
New or modified archives must be indexed! To do this we use another command called
To go back and check the contents of your library, you can run
ar with the
Let’s see this all in action.
Give yourself a pat on the back because you’ve created your first static library! Now to use it, simply add the library’s name during compilation using the following flags:
gcc main.c -L. -lyourlibraryname
Let’s look closer at this syntax:
-Ltells linker that libraries might be found in the given directory, in this case we use
.to tell compiler to look in the current directory
-lis used along with the library name — note how the
.aextension is omitted here!
In computer science, a static library or statically-linked library is a set of routines, external functions and…en.wikipedia.org