working with static libraries in c

Jack Gindi
3 min readOct 8, 2018

So you’ve figured out how to write programs and functions in C. The next logical step is to put those functions together, right? Well, if you try to do that right now, you’ll be spending endless amounts of time writing and rewriting functions for your shiny new project.

Which is where libraries come in.

A library is a file that contains object-file versions of functions for a project in one convenient little package. Instead of doing something like this:

one could easily create a library in order to shorten the time it takes to link files during compilation, and to reduce the number of characters you type — which means you’re less likely to make a mistake and have to search your command for it, or worse, have to rewrite the whole thing all over again.

While there are two different types of libraries for C (static and dynamic/shared), this article will explore one of the two — static libraries.

why use libraries?

“But header files are enough!” you say. “I can just reference all my functions in the header and link from there!”

But what happens if you have a program with fifty, a hundred, hundreds of functions?

Libraries.

Where you had hundreds of files, there is now a single (or maybe two or three) library.

how do i do it?

Libraries are surprisingly simple to create. The main steps are compilation, archiving, and indexing, followed by the final compilation of your program.

compilation, pt 1

In order to create a library of functions, first we need a folder full of them.

jack@ubuntu:~/0x08-static_libraries$ ls *.c
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
0-memset.c 100-atoi.c 1-memcpy.c 2-strchr.c 2-strncpy.c 3-puts.c 3-strspn.c 4-strpbrk.c 6-abs.c

Then, we run gcc with the -c flag.

gcc -c *.c

You’ll notice that the files that gcc outputs have the extension .o, which signifies an object, or machine, code file. These are the files we need in order to create our library. While you could include single object files in compilation, we’re looking at a folder with a large number of these files in it, so it would be much easier to create a library of those functions.

archiving

The ar (or “archive”) command is what we use to create the library. by using the flags -r and -c, we tell the archiver to replace any old object files in the library with their newer versions, and create the library if it does not exist.

ar -rc libmylib.a *.o

In order to make things faster (and less prone to error), you can use *.o to collect all .o files in the current directory.

indexing

Indexing organizes a library to do two things: speed up symbol lookup, and make sure that the order of symbols will not affect compilation. We can use the ar command’s -s flag, or use the ranlib command, in order to index our archive.

ar -s libmylib.aranlib libmylib.a

compilation, pt. 2

To use our library in a program, we simply have to include it in the final compilation using gcc.

gcc main.c -L. lmylib

The -L flag tells gcc to look in a directory — here we’ve signified the current working directory using ‘.’.

You’ll notice an odd format for the filename. When you use “l” followed by the library name, with the prefix “lib” and extension “.a” removed, gcc will automatically add the removed parts back when searching. It is important to note that dynamic libraries have preference over static, so if you have a dynamic library with the same name as a static library, the dynamic one will be the included file.

diving into our new library

Typing nm libmylib.a into the standard input of your console will now show you what’s inside your library. ar -t libmylib.a will also show you the same files.

I hope you have learned a little about static libraries in C, and why they are a necessary tool to have under any programmer’s belt.

--

--