Static libraries in C, why do they matter❓

A H
5 min readFeb 12, 2018

--

What is a static library?

A static library is a collection of functions, structures, binary code and other bits of information packaged in such a way that makes them more accessible as they are both reusable and portable.

Important notes 💡

  • Generally static libraries are indexed
  • Static libraries are beneficial to cut back on time during the linking phase of a programs compilation

Why use static libraries?

Static libraries in C are beneficial for a couple of reasons, here are the 3 most important ones that I’ve found:

  • For code that is often used but rarely changed, libraries are ideal — an example of such code would be the printf() function or atoi(), which allow a programmer to print to standard output with arguments or convert a char/string to an int respectively. By using a library which contains both functions, the programmer doesn’t have to worry about creating a standalone function in their program each time.
  • Using a library can be quicker than using individual functions because they can be indexed. Indexing a file creates symbols which the linker can take advantage of, making it much faster to use a library then say, having separate object files which each need to be accessed individually.
  • Libraries allow you to create multiple object files and store them in one package that can be used on any system without the need for the original source file. #LibrariesForTheWin.

How does a static library work?

A static library works by copying an object or a series of object file’s binary code. It does this by creating an archive which can be used on any machine, even one where the original source files don’t exist as mentioned above.

Important notes 💡

  • Static libraries are very similar to .zip files and are the precursor to .tar files
  • You can create an object file by compiling a C program using gcc. For example, you could run the command: gcc -c hello.c to compile a C program called hello, the output of the command would be hello.o
  • Object file extensions can vary, for C programs, they end in .o
  • Static libraries are especially beneficial during the linking phase just before the executable of a program is generated

How do you create a static library?

You can create a static library by running the command ar in your terminal followed by r to replace object files with new object files (should you be updating your library with new source code) and c to actually create the archive itself — the last argument ar needs in this case is the object files you’d like to be contained within the archive or those that need to be updated.

Using the example above, we’ll create an archive with two programs, hello.c and world.c:

As you can see above, I’ve first created object files for hello.c and world.c and then listed my current directory’s contents to verify they existed. I then created an archive or static library called lib.a which contained the object files hello.o and world.o.

Now that we have a static library created, we’ll want to index it. You can do so by running the command ranlib like so:

Above, I’ve ran the command ranlib and passed it the argument being the name of the library we created above, in this case lib.a. Then, in order to verify we indexed lib.a correctly I ran the additional command nm, which lists all symbols (an artifact of being indexed) contained within the archive.

Important notes 💡

  • Always remember that you need to create an object file to pass into an archive
  • Make sure to index your static library — if you don’t, the benefits of having a static library are less venerable
  • Remember to check that your archive was indexed by using the nm command

How do you use a static library?

To use a static library, you need to add the library’s name to the list of object files given to the linker at compilation. You can do this by using the flag -L. to look for your library in the current directory followed by your library’s name.

Here’s an example:

As you can see above, I’ve first created the archive using the ar -rc command followed by the name of the library and the object files we’d like to be contained within it. I’ve then ran the ranlib command to make sure lib.a is indexed. Lastly, I ran the gcc command followed by the new empty .c file called hello_world.c, the arguments I passed were -L. to look for the library in my current directory, the library name, lib.a and -o to specify what I would like the output file to be named. I then ran the program hello_world to verify everything went smoothly.

Important notes 💡

  • Be sure to use the -L. argument to look for your library in your current directory
  • Always run your program to make sure everything went smoothly

Parting notes…

Creating static libraries is a useful tool in any developers arsenal. Static libraries help to increase compile time speed/efficiency by allowing the linking process to use indexed object files. Static libraries are also a good way to share functions across a large project or allow other users of a specific program to access functions created by one person.

See any corrections? Feel free to contact me on twitter at @wn_gc, or via email at a.catawampus@gmail.com.

--

--