Static libraries in C

Joudirim
4 min readFeb 28, 2021

--

Hello , in this blog we are going to talk about static libraries in C.

What are static libraries, How they work, How to create them and How to be used.

What is a library in C ?

A library is a collection of items that you can call from your program.

It obviously has a lot of advantages, not least of which is that you can save much time by reusing work someone else has already done and be more confident that it has fewer bugs.

A library is exactly like an executable, except instead of running directly, the library functions are invoked with parameters from your executable.

Why use libraries in C?

Libraries in C are not unlike public libraries in cities, towns, or neighborhoods. A public library provides access to a multitude of information in various media forms to the public for access and use.

Functions in a C library can be used and accessed by programmers to create several different programs.

As a programmer, you may find yourself using the same function or functions repeatedly.

In this case, it is best to put this function or functions in a library to speed up the compilation of the program.

C libraries store files in object code; during the linking phase of the compilation process files in object code are accessed and used.

It is faster to link a function from a C library than to link object files from a separate memory sticks or discs.

What are static libraries and how they work?

Static libraries are simply a collection of ordinary object files; conventionally, static libraries end with the ``.a’’ suffix. This collection is created using the ar (archiver) program.

Static libraries permit users to link to programs without having to recompile its code, saving recompilation time.

Note that recompilation time is less important given today’s faster compilers, so this reason is not as strong as it once was.

How to create a static library ?

To create a static library we need to compile our library code into an object file so we tell GCC to do this using -c:

$ gcc -c *.c

Now all the .c extension files( C files) in the current working directory have been converted in to their respective object files.

Once we have object file(s), we use the ar command to create our static library.

ar (the archiver), is a Unix utility that maintains groups of files as a single archive file.

$ ar -rc libholberton.a *.o

So ar is going to :

Create an archive (option c).

Insert the objects, replacing older files where needed (option r) .

When files are added to a library, now it needs to be indexed, which is done with the command ranlib.

ranlib makes a header in the library with the symbols of the object file contents.

This helps the compiler to quickly reference symbols.

$ ranlib libholberton.a

If we want to see the contents of our library, we can use the ar option -t.

ar -t libholberton.a

We can also see the symbols in our library, using the command nm, which lists each symbol’s symbol value, symbol type, and symbol name from object files.

nm libholberton.a

To use the static library call on the library during the linking phase of program compilation.

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

‘-L’ specifies the library path.
‘-l’ goes before the library name.

You are able to run the executable program.

$./main

How are static libraries used?

We have created a static library and can now use it.

First you need to write a program:

#include "holberton.h"
int main(void)
{
_puts("\"My Static Library is working.\"");
return (0);
}

In the header: “holberton.h” contains function and function definitions used to tell the compiler how to call functionality. It contains “data types and constants used with the libraries”(geekforgeeks).

When we compile the file we call the library:

$ gcc main.c -L. -libholberton -o quote

Now we can run the executable program:

$ ./quote

If the library is working properly the output should be:

"My Static Library is working."

--

--