Suzzallo Library Reading room at the University of Washington (Photo: Me)

Early in one’s development as a software engineer, one’s code begins to scale up in complexity and length. Eventually this can to lead to serious latency at either compile time or (horror!) at run time. In order to lighten the load on the user, the developer and the machine, one must find more uniform, parsimonious ways to drive the digits to do one’s bidding. Libraries, both static and dynamic, are that answer. That might hint at the “Why?” of libraries, but not

  • What they are

The rest of this short article will attempt to clarify all of these points.

Note: This article will only touch on static libraries. Dynamic libraries will have to be addressed at another time.

What is a Static Library? and Why?

Put simply, a Static Library is an indexed collection of compiled object files to be linked into the final executable produced by a compile command. To understand this more fully, let’s walk through the final few stages of compilation. After the compiler completes the assembly phase, the linker must draw together all of the object code (binary) into a an executable. Without a library this could mean using a large array of individual source code files. This could become tedious and fertile ground for user error. It also can lead to duplications in memory that can bog down compile time. If one has created a large base of custom functions, each in their own file, when it comes to run the compile command could become very cumbersome even to simply type. For example:

gcc 0-isupper.c  0-strcat.c  1-isdigit.c  1-strncat.c  2-strlen.c   3-islower.c  3-strcmp.c  4-isalpha.c  5-strstr.c  9-strcpy.c  _putchar.c main.c -o manisthisalongcommandline 

Whereas the same code with a created and indexed library could be rendered as:

gcc main.c -L. -lholberton -o ahmuchbetter

What’s more, any of those functions will only be able to be called from the folder where the main function to be compiled is. In a word: portability. Being able to call the functions from anywhere within the system would be a huge savings in time and memory.

How Does a Static Library Work?

A static library works by gathering together code that has already been converted to object code and making it easily accessible by memory address. Since the objects inside the library are object code (i.e. they have already been compiled into binary) the only code remaining to be converted to assembly and binary at main compile time is the main file, barring any other custom code outside the library that might be used. After compiling the remaining source code in the assembly phase, the linker then seeks out the object code as referenced in the library index.

How a Static Library is Made?

There are two Linux shell commands to know in order to create and index libraries. Before creating the library, though, the source code must be compiled in order to be included. For an example let’s assume all the files to be included in the library were in their own folder. This will allow us to use a CL expansion to compile all .c files in the folder to object files. For example:

$ gcc -c *.c

Next all of those object files can be included in a new library with the command ar.

ar rc liball.a  *.o

Once the library has beeb created it can be indexed by using the ranlib command.

ranlib liball.a

How to Use a Static Library

As we saw above the way to include a static library is to include a call to the library in your compile command.

gcc main.c -L. -lholberton -o ahmuchbetter

I am a software student from Tulsa OK