STATIC LIBRARY in C Programming Language

Mr. T
5 min readJan 16, 2023

--

There are four phases in the compilation of a C program.

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

The standard library saves programmers from having to reinvent the wheel.

Bjarne Stroustrup

What is a Library in C Programing Language?

A library is a file that contains several object files that can be used as a single entity in a linking phase of a program.

There are two types of libraries in the C programing language.

  1. Shared/Dynamic Library
  2. Static Library

But today we are going to talk about Static Library.

With a library, you are free, not confined by temporary political climates. It is the most democratic of institutions because no one — but no one at all — can tell you what to read and when and how.

Doris Lessing

What is a Static Library in C Programing Language?

A static library is a collection of object files that are combined into the program during the linking phase of compilation, and are not relevant during runtime.

The compiler creates an object file (.o) from the source file (.c) during the compilation process.

When the object file is created, the compiler then calls on the linker to copy the code of the library to our object file.

How to Create a Static Library in C Programing Language

To create a static library, we tell the compiler to compile all our C source files(*.c) into object files(*.o) without linking.

Let’s assume we have three C files called field.c, file.c, and name.c.

To compile the above C files, let’s use a GCC compiler. We are using the command below to compile our C files.

gcc -c *.c

We now have our C files compiled into object files, as shown below.

-c: It compiles and assembles but does not link.

*.c: It matches all files in the current working directory with the “.c” extension.

We then use the archiver (ar) program to combine all our object files(*.o) into one static library.

archiver (ar): It creates a static library.

The command below is used to combine object files into one static library.

ar -rc libmy.a *.o

c flag in rc: It tells the archiver(ar) to create the static library if it doesn’t exist.

*.o is our object files that have been created.

In our case, a static library, libmy.a, has been created because it did not exist.

r flag in rc: It tells the archiver(ar) to insert the object files or to replace the object files in the library with the new object files if it exists.

*.o: It matches all the object files in the current working directory and it puts all object files in the current directory into the library name we created.

After the creation or modification of an archive, we have to index it.

What is the use of the Index?

It is used by the compiler to speed up symbol lookup inside the library and to ensure that the order of the symbols in the library won’t matter during compilation.

We can create or update the index through the following:

  1. Using the command ranlib: ranlib libmy.a
  1. Or adding the flag -s to the ar command: ar -rcs libmy.a *.o

ar command with the -t flag lists content inside the library. In our case, these are object files.

ar -t libmy.a

nm command lists symbols from object files.

nm libmy.a

How to use Static Library

“Libraries allow children to ask questions about the world and find the answers. And the wonderful thing is that once a child learns to use a library, the doors to learning are always open” — Laura Bush

We can now use our static library, libmy.a in a program. This is done by adding the library’s name to the object file(s) given to the linker.

The command below can be used to create our final executable program.

gcc new.c -L. -lmy -o new

It creates a program using the object file new.o, and any symbols it requires from the ‘my’ static library.

Flags Description:
-L : It specifies the path to the given libraries. The ‘.’ in the ‘-L’ refers to the current directory.
-l : It 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 out for.

Now we can run the executable program ‘new’.

“Everything you need for better future and success has already been written. And guess what? All you have to do is go to the library.”

Henri Frederic Amiel (1821–1881. Swiss moral philosopher, poet, and critic)

That was all I had for you today on Static Library and I hope you learned a lot.

Thank you for reading this article.

Happy Coding!

References:

https://docencia.ac.upc.edu/FIB/USO/Bibliografia/unix-c-libraries.html

--

--

Mr. T

​Software Engineering Student - ALX. I write about Programming because I think most articles that leave programming out misrepresent life. ​