All about static libraries in C

Issam Sebri
3 min readMar 1, 2020

--

Photo by Yury Nam on Unsplash

Libraries are very important in C because the C language supports only the most basic features that it needs. For example, C doesn’t contain input-output (I/O) functions to read from the keyboard and write to the screen. Anything that extends beyond the basics must be written by a programmer. If the chunk of code is useful to multiple different programs, it’s often put into a library to make it easily reusable.

Why we use them?

A library in C is a collection of per-compiled and non-volatile routines used by programs, this routines can be used often to minimize development and testing procedure and speed-up the compiling time, almost a library is a bunch of functions that seems reusable.

In C (and C like programming language) we distinguish two types of libraries:

Static libraries: Compiled to object code by the compiler and the objects merged into a library by the linker tool.

Dynamic linked libraries: Is may compile in two stage, first at the compiling in linking phase when is treated as static libraries, Instead during program starting when shared libraries serve the dynamic loader.

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 or .obj or anything else. A static library is basically a collection of object files, kind of like a .zip file but probably not compressed. 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.

How to create static libraries?

So let’s get our hand dirty and try making a static library from scratch. As a developer known a string length is one of the most common function you need, of course we don’t need to build a “string length” function every time we build a program (Forget for now that’s included in the <string> library).

As we mention before, a static library is a collection of object functions so let’s compile our “_strlen” function to object file:

gcc -o strlen string_length.c

By executing the last command we generate the strlen.o object file ready to be collected in a static library. The basic tool used to create static libraries is a program called 'ar', for 'archiver'. This program can be used to create static libraries (which are actually archive files), modify object files in the static library, list the names of object files in the library …

ar -rc mylibrary.a strlen.o

here we finish making our library “mylibrary.a” and is ready for using.

How to use static libraries?

In order to use a library we need a driver program with main feature help to call our library functions:

The next step is compiling our program with linked library “mylibrary.a”. and to make this option we need to compile with the “-l” flag.

The linker searches a standard list of directories for the library, which is actually a file named “mylibrary.a”. The linker then uses this file as if it
had been specified precisely by name.

gcc main.c -L. -lmylibrary -o string_length

When we discussed static libraries we said that the linker will try to look for a file named ‘mylibrary.a’. We lied. Before looking for such a file, it will look for a file named ‘mylibrary.so’ — as a shared library. Only if it cannot find a shared library, will it look for ‘mylibrary.a’ as a static library. Thus, if we have created two copies of the library, one static and one shared, the shared will be preferred.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Issam Sebri
Issam Sebri

No responses yet

Write a response