Static libraries in c language

Mohamed Foued Jenni
4 min readOct 10, 2020

--

Everything comes for a reason

One of the problems with developed programs, that they tend to grow larger and larger which bring us to using functions .

The first reason is that they can be used to avoid repetition of commands within the program.

The second reason for careful use of functions is to help define a logical structure for your program by breaking it down into a number of smaller modules with obvious purposes.

It is this point where we start thinking about combining out source code into small units of related files, that can be managed with a separate make-file also called library.

What’s a library ?

In a general sense libraries are collections of precompiled functions that have been written to be reused by other programmers.

A library is a file containing several object files, that can be used as a single entity in a linking phase of a program. Normally the library is indexed, so it is easy to find symbols (functions, variables and so on) in them.

Unix systems (as well as most other modern systems) allow us to create and use two kinds of libraries — static libraries and shared (or dynamic) libraries.

Static libraries are just collections of object files that are linked into the program during the linking phase of compilation, and are not relevant during runtime.

Shared libraries (also called dynamic libraries) are linked into the program in two stages. First, during compile time, the linker verifies that all the symbols (again, functions, variables and the like) required by the program, are either linked into the program, or in one of its shared libraries.

There are a number of conventions for naming libraries and telling the compiler where to find them that we will discuss in this lecture. A library filename always starts with lib. The last part of the name determines what type of library it is:

.a: static, traditional libraries. Applications link to these libraries of object code.

.so: dynamically linked shared object libraries. These libraries can either be linked in at runtime but statically aware or loaded during execution by the dynamic link loader.

Compilation phases

How does library work?

For example, the standard C library, ‘libc.a’, is automatically linked into your programs by the “GCC” compiler and can be found at /usr/lib/libc.a. Standard system libraries are usually found in /lib and /usr/lib/ directories. Check out those directories. By default the GCC compiler or more specifically its linker needs to be directed to which libraries to search other than the standard C library — which is included by default.

Creating and using a library

Here is a look to my functions using “ls” command:

$ ls -1
0-isupper.c
0-memset.c
0-strcat.c
100-atoi.c
1-isdigit.c
1-memcpy.c
1-strncat.c
2-strchr.c
2-strlen.c
2-strncpy.c
3-islower.c
3-puts.c
3-strcmp.c
3-strspn.c
4-isalpha.c
4-strpbrk.c
5-strstr.c
6-abs.c
9-strcpy.c
_putchar.c

1- Compile all the .c files into object files using “gcc” command:

$ gcc -Wall -pedantic -Werror -Wextra -c *.c$ ls -1
0-isupper.c
0-isupper.o
0-memset.c
0-memset.o
0-strcat.c
0-strcat.o
100-atoi.c
100-atoi.o
1-isdigit.c
1-isdigit.o
1-memcpy.c
1-memcpy.o
1-strncat.c
1-strncat.o
2-strchr.c
2-strchr.o
2-strlen.c
2-strlen.o
2-strncpy.c
2-strncpy.o
3-islower.c
3-islower.o
3-puts.c
3-puts.o
3-strcmp.c
3-strcmp.o
3-strspn.c
3-strspn.o
4-isalpha.c
4-isalpha.o
4-strpbrk.c
4-strpbrk.o
5-strstr.c
5-strstr.o
6-abs.c
6-abs.o
9-strcpy.c
9-strcpy.o
_putchar.c
_putchar.o

2- Create the static library with the program command “ar”(archiver):

$ ar -rc libc.a *.o$ ls -1
0-isupper.c
0-isupper.o
0-memset.c
0-memset.o
0-strcat.c
0-strcat.o
100-atoi.c
100-atoi.o
1-isdigit.c
1-isdigit.o
1-memcpy.c
1-memcpy.o
1-strncat.c
1-strncat.o
2-strchr.c
2-strchr.o
2-strlen.c
2-strlen.o
2-strncpy.c
2-strncpy.o
3-islower.c
3-islower.o
3-puts.c
3-puts.o
3-strcmp.c
3-strcmp.o
3-strspn.c
3-strspn.o
4-isalpha.c
4-isalpha.o
4-strpbrk.c
4-strpbrk.o
5-strstr.c
5-strstr.o
6-abs.c
6-abs.o
9-strcpy.c
9-strcpy.o
libc.a

As you can see we got new file “libc.a”, we can list it’s content :

$ ar -t libc.a
0-isupper.o
0-memset.o
0-strcat.o
100-atoi.o
1-isdigit.o
1-memcpy.o
1-strncat.o
2-strchr.o
2-strlen.o
2-strncpy.o
3-islower.o
3-puts.o
3-strcmp.o
3-strspn.o
4-isalpha.o
4-strpbrk.o
5-strstr.o
6-abs.o
9-strcpy.o
_putchar.o

3- Index the library (archive) by using the command “ranlib”:

$ ranlib libc.a

Now that our library is up and running we can finally start using it by compiling our main program “main.c” with the library “libc.a”(at this stage we don’t use the prefix “lib” and the extension .a).

$ gcc main.c -L. -lc -o alphabet
$ ./alphabet
abcdefghijklmnopqrstuvwxyz

And that’s it for today’s story, see you soon.

--

--

Mohamed Foued Jenni
Mohamed Foued Jenni

Written by Mohamed Foued Jenni

Software engineer | AR/VR student

No responses yet