About C static libraries

What is a C library?

4 min readJul 15, 2019

--

A library is a collection of items that you can call from your program. A library is a file containing several object files, that can be used as a single entity in a linking phase of a program.

Why you should use libraries ?

  • They work !
  • It saves considerable development time !
  • The functions are portable.

Differences between header file and a library.

The files that tell the compiler how to call some functionality (without knowing how the functionality actually works) are called header files. They contain the function prototypes. They also contain Data types and constants used with the libraries. We use #include to use these header files in programs. These files end with .h extension.

Library is the place where the actual functionality is implemented i.e. they contain function body

This is how a header file looks:

But we have two kinds of libraries:

Static : Static libraries contains object code linked with an end user application and then they become the part of the executable. These libraries are specifically used at compile timewhich means the library should be present in correct location when user wants to compile his/her C.

Shared or Dynamic : These libraries are only required at run-time i.e, user can compile his/her code without using these libraries. In short these libraries are linked against at compile time to resolve undefined references and then its distributed to the application so that application can load it at run time.

How they work?

As you may know, when you compile a source file you get an object file. Depending on your platform its extension may be .o . A static library is basically a collection of object files. The linker, when trying to generate an executable tries to resolve the referenced symbols, i.e. locate in which object file (be it in a library or otherwise) they are defined and links them together. So, a static library may also contain an index of defined symbols in order to facilitate this. Exact implementation depends on the specific linker and library file format but the basic architecture is as mentioned.

Now lets see how to create one !

First make sure you have your functions and the header file with the prototypes. This is the ls output for this example:

Now lets get the objects for the library. You can use this compilations flags gcc -Wall -pedantic -Werror -Wextra -c *.c or just the gcc -c *.c. And you should obtain an output like this:

In order to create the library we need to copy all those objects files in it. For that we can use the command ar -rc libmedium.a *.o

This command creates a static library named ‘libmedium.a’ and puts copies of all the object files in it. If the library file already exists, it has the object files added to it, or replaced, if they are newer than those inside the library. The ‘c’ flag tells ar to create the library if it doesn’t already exist. The ‘r’ flag tells it to replace older object files in the library, with the new object files.

After an archive is created, or modified, there is a need to index it. This index is later used by the compiler to speed up symbol-lookup inside the library. For that we can use the ranlib command: for this example ranlib libmedium.a

And thats its lets take a look !

Now we have one, how can we use it ?

After we created our archive, we want to use it in a program. This is done by adding the library’s name to the list of object file names given to the linker, using a special flag, normally ‘-l’. Here is an example:

gcc main.o -L. -lmedium -o program

This will create a program using object file “main.o”, and any symbols it requires from the “util” static library. Note that we omitted the “lib” prefix and the “.a” suffix when mentioning the library on the link command. The linker attaches these parts back to the name of the library to create a name of a file to look for. Note also the usage of the ‘-L’ flag — this flag tells the linker that libraries might be found in the given directory (‘.’, refering to the current directory), in addition to the standard locations where the compiler looks for system libraries.

--

--