C Static Libraries

Yashey Mateen
4 min readJul 15, 2019

--

Image Credit: https://images.unsplash.com/photo-1524995997946-a1c2e315a42f?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80

A C library is a file that consists of various object files that can be used as a single entity during the linking phase while executing a program/function. Since the library has fewer files to look for and open, and since the object files are ordered and indexed, it makes the linking process much more efficient and faster. The library functions are invoked using parameters from your executable file.

Basically, if you have a bunch of functions that you use during executing of a program, you can save time by creating a separate library of those functions, and that way if someone else uses your program, they will also be able to save time by reusing your work.

So how do they work? And how do you create one?

Image Credit: https://www3.ntu.edu.sg/home/ehchua/programming/cpp/gcc_make.html

As you can see above, a static library is linked to your executable code directly. This way you can use the object files in your library right in your executable program. The library remains the same until you recompile your program, thus why it is called the static library. A dynamic library on the other hand has object files that are not directly inserted into the executable file, instead the program in the system checks out which libraries that are shared are linked to the program, loads them into memory, and then attaches them to a copy of the program in memory. The libraries end with a prefix of .lib or .a.

Creating a Static Library

In order to create a static library, you must first have the c files for which we we can convert to object files. Here we have 3 c files named 1.c, 2.c, & 3.c. Using the command “gcc -c *.c”, we can convert all the c files, as indicated by the wildcare that includes all c files, into object files of 1.o, 2.o, & 3.o.

The tool we use to create the library is called ar for archiver. It can be used to create static libraries, modify the object files in that library, etc. We use the command “ar -rc testlib.a *o” to archive our library called “testlib.a” and include all the object files in the directory as indicated by the *.o wildcard. The c flag tells us to create the library if it already doesn’t exist, and the r flag tells us to replace the older object files with the new ones.

After we create the library, we have to index it using the command ranlib. This way, a header is created in the library with the symbols of the object file contents, similar to a table of contents. The compiler is able to quickly reference symbols from the library, especially if there is a large amount of object files.

To see the contents of the library we created, we can use the command “ar -t testlib.a”. Here we see that the 3 object files in our library are listed when using this command.

Using The Static Library

In order to use the static library, we have to invoke it as part of the compilation and linking process when we create our executable program. We can use the command: gcc main.c -L. -ltestlib -o main, followed by: ./main.

The above command uses the -l argument without the lib prefix and extension, and the program tacks it back on during compilation. The -L flag specifies the path to the directory. When we run the executable program using ./main, we can link the static library. Thus, a static library is not required at runtime when your execute your program, and so it’s not necessary to include them when distributing your executable. Linking a static library is more efficient and speeds up the process rather than linking individual source files.

One thing to remember is that if you update your library, you have to recompile your program into a new executable file. In addition, sometimes it is more efficient to use a dynamic, or shared, library since every program that uses the static library will maintain a copy of that executable and in some cases these may be inefficient during debugging.

--

--

Yashey Mateen

Software Developer, Business Consultant, with a head in the clouds.