C Static Libraries

Dennis Pham
4 min readJul 15, 2018

A library is a type of collection of services that are made available to other parts of a program. This helps with program design by making it much more modular and reusable rather than placing every component in a very large program. The goal is to have high cohesion and low coupling which means to have related function work towards a common goal as well as make them as independent as possible with little influence upon each other. In C, a library is a collection that consists of functions and data types the can be re-implemented.

Why is it called a static library? Although they are still distinct files within the library, they are statically linked. The object files that are linked together in the library can’t change once it’s been done and we would need to archive it again in order to update the library. We would have to rebuild it. Another option would be to use a dynamic library and dynamically link them together using several object files stored in separate files. The advantage of dynamically linking allows for more configurations at linking with the tradeoff of having lots of separate files.

We use static libraries in particular because it’s more strict and the commands will check the file types we use. We also get a fast executable that is easy to move with smaller modules and low coupling. This allows the object files inside of a static library to remain distinct.

Today, we are going to take a look at static linking and how static libraries work by making our own library archive with object code files. The library is called during the linking stage of compilation.

In order to create the static library file, we first need to compile all of our files for assembly, but not link them for executing. In this case, we want .o object files for each of our C files. Doing this will create .o object files for each of our C programs. We will use the .o files for archiving it into a shared library.

gcc -c *.c   /* -c flag compiles or assembles the source file(s) but does not link */

We now can use the archive command: ar. According to Unix’s manual, ar can create, modify, and extract from archives. We’re going to use it right now to create. We’re going to store the object files into an archive called libholberton.a

ar -rt libholberton.a

We can also use other flags along with ar. -r will update the file if it already exists. -t will list the contents of the library which is very useful for keeping track of your object files. -s can create an index at the start of the .a file. The naming syntax is also the same for static libraries: lib<name>.a

When making a library, we sometimes need to double check it in detail. We’re going to use another command called nm, which will allow us to list symbols (both type and value) from object files. The “T” symbol type means that object file contains that function. The”U” symbol type reveals usage of another function. When there are no symbols attached to a file name, that object file will not run and tells us we need to troubleshoot it.

nm libholberton.a

In this case, all of our symbols are correctly displayed. We should now index our library. We can do this with a command called ranlib, which generates an index to the archive. This index will store symbols in the library and whenever we execute using the library, the compiler will run much faster because it can now look up those symbols instead of each file. This becomes very important as programs and libraries get larger. Every time we archive our library, it’s always a good idea to also index it.

ranlib libholberton.a

Our library is ready and we can now link. Let’s now produce an executable using gcc.

gcc main.c -L. -lholberton -o main
  • o renames our executable to main. -l<name> is a shortened version of lib<name>.a which is very helpful when using several library archives. -L is a path back to the library; we use the . to specify the current directory. We can also search for any other directory using -L and a path name.

Our library is complete. We can now execute main.

--

--