What the Hell is a C static libraries?

Hello everyone,so today we are going to learn about static libraries,why use a libraries,how they work,how to create them and how to use them in your code.We will some differents example,but before that,let’s talk a little bit history.

A brief History of C programming langage

The C programming language came out of Bell Labs in the early 1970s.

C programming language was developed in 1972 by Dennis Ritchie at Bell laboratories of AT&T(American Telephone & Telegraph), located in the U.S.A.

Dennis Ritchie

Dennis Ritchie is known as the founder of the c language.It was developed to overcome the problems of previous languages such as B, BCPL,etc…

Iniatlly,C language was developed to be used in Unix operating system.It inherits many features of previous languages such as B and BCPL.

Features of C programming Language:

Let us see some features :

1-Procdedural Language: In a procedural language like C step by step predefined instructions are carried out. C program may contain more than one function to perform a particular task.That means ,instructions can be divided into smaller part known as functions in order for the computer to perform.

2-Fast and Efficient: C programming language as the been middle-level language provides programmers access to direct manipulation with the computer hardware but higher-level languages do not allow this.That’s one of the reasons C language is considered as the first choice to start learning programming languages.It’s fast because statically typed languages are faster than dynamically typed languages.

3-Modularity: The concept of storing of C programming language code in the form of libraries for further future uses is known as modularity.C language has it’s own library to solve common problems like in this we can use a particular function by using a header file stored in its library.

4-Statically Type: C programming language is a statically typed language. Meaning the type of variable is checked at the time of compilation but not at run time. Means each time a programmer type a program they have to mention the type of variables used.

5-General Purpose Language: From system programming to photo editing software, C programming language is used in various applications. Some of the common applications where it’s used are as follows:

  • Operating Systems:Windows,Linux,iOS,Android,etc.
  • Databases :PostreSQL,Oracle,MySQL,etc.

6-Easy to Extend: Programs written in C language can be extended means when a program is already written in it then some more features and operations can be added into it.

What’s a library and how it work

Before starting to talk about the subject matter, let us take a brief look at the compilation phases of a C program.
There are basically four phases:

  1. Pre-processing
  2. Compilation
  3. Assembly
  4. Linking

A library is a collection of code routines (functions, classes, variables, and so on) that can be called upon when building our program, so instead of writing it ourselves, we can go and get it from something that has already been written and optimized. That is where the idea behind libraries comes from. We are reusing blocks of codes that have come from somewhere else.

For example, if you want to use the “printf()” function,the header file “<stdio.h>” should included.

If you try to use “printf()” without including the “stdio.h” header file,you will get an error.

We have two kinds of libraries:

  • static libraries
  • dynamic libraries

But here we will only talk about the static libraries.

A static library is file containing a collection of object files (*.o) that are linked into the program during the linking phase of compilation and are not relevant during runtime.

The diagram above show us when a program is compiled,the compiler generates an object file from a source file.After generating the obeject file,the compiler also invokes the linker.The role of the linker , in this case, is to copy the code of the library to our object file.

Static libraries are just a collection of object files that are merged by the linker with another object file form i final executable.

Conventionally ,they start with “lib” and end with “.a” or “.lib”.

How to create a static libraries?

To create a static library, we need to specify to the compiler, which is GCC in our case, that we want to compile all library codes (*.c) into object files (*.o) without linking. To do that we are going to use the command below.

Flags description:

-c: Compile and assemble, but do not link.
-Wall, -Werro and -Wextra: These aren’t necessary but they are recommended to generate better code.

Note that the “*.c” matches all files in the current working directory with the “.c” extension.

For example, let us take two c files, add.c, and mul.c which make respectively the addition and the multiplication of two integers, and a header file that contains the prototypes of these functions. The picture below shows the output generated after using the command.

Once we have object file(s), we can now bundle all object files into one static library.
To create a static library or to add additional object files to an existing static library, we have to use the GNU ar (archiver) program. We can use a command like this:

This command creates a static library named “libname.a” and puts copies of the object files “add.o” and “mul.o” in it. The ‘c’ flag tells ar to create the library if it doesn’t already exist. The ‘r’ flag tells it to insert object files or replace existing 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 and to make sure that the order of the symbols in the library will not matter during compilation. There are two ways to create or update the index. The first one is, by using the command ranlib.

The picture below shows the execution of this commands on our example

In order to list the names of the object files in our library,we can use the ar command with -t flag

How to use them?

Now our static library “libname.a” is ready to be used. we can use it in a program. This is done by adding the library’s name to the object file(s) given to the linker. First, let us create a C source file that uses the above created static library.

Now we can use the command below to create our final executable program:

This will create a program using the object file “main.o”, and any symbols it requires from the “name” static library.

Flags description:
-L : Specifies the path to the given libraries (‘.’ referring to the current directory)
-l : Specifies the library name without the “lib” prefix and the “.a” suffix, because the linker attaches these parts back to the name of the library to create a name of a file to look for.
All we have to do now is to run our program.

End that’s it,i hope you learned something,ill be happy to see your libraries

Happy coding!!