STATIC LIBRARIES IN C

Marco Niño
4 min readOct 17, 2019

The moment a C program is compiled, just before the linking we can find an object file or object code which then the linker takes care of for making functions defined in headers available (putchar, printf, strlen, etc.), this can be acomplished in two ways; the first is copiying the code of each of this functions into the program or by making it available during run-time.

This time we’ll focus on the first case:

When this happens, the library is considered a Static Library and thus, are statically linked to the program, making it bigger as the linker has to create larger binary files so, more HHD space is needed. The libraries created by this method have an .a exension in linux and .lib in Windows.

To create a static library in Unix or Unix-like operative systems, we’ll need:

1. To create the C functions that will be in the library.
2. Create a header file for the prototypes of the functions.
3. Compile the library files.
4. Create the library file.

1. An empty library is like an empty canvas.

First of all we need to make all the files that are going to be in the library, for this any normal C file will do.

/** Filename: Hello_there.c */
#include <stdio.h>
#include "My_header.h" /** Remember this the double quotes are important*/
void Hello_there(void)
{
printf("Hello There");
}

This simple file can be used as an example. You can add as many programs as you like.

2. Without the head, the body falls appart.

Next we’ll create our header file, the header is a file that contains all the prototypes of any function we need to store in the library, all headers have .h extension.

/** Filename: My_header.h */
#indef <HEADER_NAME>_H /** This is important because we only need */
#define <HEADER_NAME>_H /** the header to be defined once. */
#include <stdio.h> /** Any other header you need */
void Hello_there(void); /** All the prototypes of our functions */#endif /** The header won't be defined each time is called */

3. If at first it doesn’t compile, just keep pushing!

We need to compile all our C files to make the object files (extension .o) that will fill our library. This is done with the command gcc.

gcc -c Hello_there.c -o Hello_there.o /** -c flag to stop before the linker phase.

That works for our only file, but what if we have a lot of .c files in our directory?

That’s a lot of files to compile…

Luckily, in cases like that we can go visit our good old friends: the wildcards.

gcc -c *.c // You can use other gcc flags like -Werror or -Wall, etc.

With this:

That’s a lot of flags…

We made ourselves a batch of brand new object files:

Note that each object file is named after its own C file.

4. Wait, this goes in the “Science” or the “Fantasy” section?

Time to create the static library, to do so we need to use the command “ar” followed by the name of the library and the name of the object files.

ar my_library.a Hello_there.o /** This is for our Hello_there example */

Seems easy enough, but wait, this only works with only one file (Hello_there) what if we need to fill our library with more that just one “book”?

We use again the wildcards:

That should do the trick.

The result should be:

And with that, our job is complete.

Now, to include the relevant files in a compilation, only the library is needed for reference:

gcc gnrl_knbi.c my_library.a /** This is another C file that uses the Hello_there.c

Static libraries offer a lot of solutions but at the same time they have some few drawbacks, for instance, what would happen if we modify something in the “books” in the library (say the “Hello_there file”?

Well, the code of the “Hello_there” file is copied in the “gnrl_knbi” file, so if we modify the former, we need to compile again for it to take effect on the later.

--

--