Introduction to Static Libraries in C Programming

Isaac Wong
4 min readFeb 12, 2018

--

Photo by Pam Riess from flickr

I remember that when I was young, I marveled at the work of a computer programmer. I thought that each time a developer made a program, she would start from ground zero, assembling each function from scratch. Then, like a craftsmen with a workshop full of jigs and other implements she accumulated over time, an experienced developer would have a treasure trove of functions and small programs that she would keep for reuse. I still think that software developers are crafty individuals, but I would come to learn that this concept of having a collection of routines accessible for reuse in programming is called a library.

Why Use Libraries?

Libraries are useful because they allow the programmer access to many pre-compiled routines, functions, and variables. This allows the programmer to keep the source code for programs in manageable chunks, and it saves her the effort of linking each asset individually. But also, powerfully, it allows the programmer to stand on the shoulder of giants, and use existing code in her own programs.

For example, the traditional first C program that one makes is a program that prints “Hello, World!” Below is the source code for such a program:

#include <stdio.h>
int main()
{
printf("Hello, World!");
return 0;
}

Essentially, this program calls upon the function, printf(), and passed the argument, “Hello, World!” to it. Note, that the novice programmer didn’t have to define the function, printf. Printf is a library function, that was referenced by the first line of code:

#include <stdio.h>

This line of code calls for standard input and output functions to be referenced in the C Standard Library, which is the library available to all implementations of the C programming language.

How Static Libraries Work

When a C program is compiled, before it is turned into the final executable program, the individual source code files are turned into object code, stored as object files. Object code is machine code, but it is not yet executable. Once the object files are made, a program called a linker can act upon different object files to form a program, or one can consolidate many object files into a static library. The user can then reference this consolidated library file in the future compliation of her programs, rather than have to worry about referencing all the program’s dependencies individually. This is not well demonstrated in simple programs, like the “Hello, World!” program that uses only one function. However, the utility of libraries shines in larger programs that reference many functions.

How to Make Static Libraries

To make a static library, one must first generate the object files from the base source code. Using the GCC, GNU Compiler Collection in the Linux terminal, the following command will compile all the C source code files and generate object files:

$ gcc -c *.c

This command invokes gcc, using the -c flag to generate object code, and applies the command to all C source code using the *.c expansion.

Afterwards, the library file named example is generated from all object files in the current directory using the command below:

$ ar -rc libexample.a *.o

This command invokes the archive command using the -rc flag to replace and create the archive. Note that the library name must be prefixed by “lib”, and suffixed by the “.a” extension.

Finally, after the library file is created, it must be indexed so that files can be located within the library archive. This is done with the ranlib command:

$ ranlib libexample.a

Now, the example library has been successfully created.

How to Use Static Libraries

To use the static library, if one has created the object file main.o that needs to use a function that she knows is referenced in the example library, she can reference it when compiling.

$ gcc main.o -L. -lexample -o program

The command above will invoke the gcc compiler to use the main.o file, and the -L flag to locate the library in the current directory, by using the “.” notation, and to use the “example” library, which is stated in conjunction with the -l flag, and to output the executable file named “program” using the -o flag.

Note that, there are no spaces between the -L flag and the directory location as well as for the -l flag and the name of the library. Also take note that like the unlike the filename of the library, the name of the library, when called, is simply the library name, without the “lib” prefix, and “.a” extension.

Conclusion

The use of libraries is a powerful feature in software development that allows programmers to effectively reuse code in other programs and become much more productive, like the craftman who is able to build using interchangeable parts. Hopefully, this article has demonstrated the utility of libraries, and exposed some of the mechanics of how a static library can be created and used in the C programming language.

--

--