Static libraries in C

Oscar Morales
3 min readJul 6, 2020

Linking programs whose object files are ordered in libraries is faster than linking a program whose object files are separeted on the disk

What is a library in C?

Library in C is a file that contains several object files (An object file is the real output from the compilation phase see Let’s talk a little bit about GCC), that can be used as a single entity when you are compiling your code. Think about a real library, when you need to solve a math problem, you look the solution in a math book. Well, library in C could contain a lot of functions that make a specific task.

In C there are two types of libraries:

  • Static library
  • Shared (Dynamic) library

Static libraries are just collections of objects files that are linked into the program during the linking phase of compilation

Shared libraries are collections of objects files that are not insert into the executable file, instead of that, a program in the system called dynamic loader takes care to separate the enough memory in your system and loads there the object files contained in the libraries

Why you should use a library?

Do you want to read a thousands of code lines? Well, libraries let you keep your code easy to read as well as for other developers that are working in the same file and for CPU: Linking programs whose object files are ordered in libraries is faster than linking a program whose object files are separeted on the disk

How libraries work?

In order to know how libraries work, we need to know how to create them and how to use them

How to create them

The command to create, modify, list and so on, a static libraries is ‘ar’ (stands for archiver: man ar). i.e:

ar -rc library.a object0.o object1.o … objectn.o

This command line creates a static library named library.a and puts copies of the object files object0.o, object1.o … objectn.o

The r option is required to insert the files of the members. i.e: object0. o, object1.o … objectn.o

The c option is required to create the archive library.a

How do you get the object files for your functions? Well, you just need to use the option -c when you are compiling your functions files. i.e:

gcc -c object0.c

This command line creates automatically a file with the same name but with .o extension. i.e: object.o

To check that your library has the functions that you want, you can use the command ‘ar’ with the option -t to see its content. i.e:

ar -t library.a

After the archive is created, or modified, it is necessary to index it. This index is used by the compiler in the linking process. We can make this index with the command ranlib (man ranlib). i.e:

ranlib library.a

Note: some operating systems already takes care of the index, so it is not necessary to run this command (What operating systems do this?)

How to use this library

This can be done by adding the library’s name to the list of object files names given to the linker using a special flag. For gcc we use the flag -l. i.e:

gcc -L. -llibrary.a -o executable

Notes that -l should be followed by the name of the library. The flag -L tells to the linker where is the library located (‘.’ current directory, but could be any other path)

--

--