Come for the static library…stay for the fun

In real life a library is a place where you can find resources to improve your knowledge on pretty much every topic of your choosing. As it is, you could go out and discover that knowledge on your own but libraries make it easier for you to profit from the hard earned knowledge from people before you. In that sense static libraries are not that different from actual libraries. In a real-life library books are organized by a specific criteria such as title, author, topic and so on. Said organization lets you interface with the book but it is the book itself that will provide you with the knowledge you so desperately seek.

On that regard also, C libraries behave in the same way as physical libraries. Your header file holds arguments, variables, return types as well as function names which help you use the library that, at the same time, holds the function implentation in machine language instructions that the computer can read.

In C language libraries are archives containt object code files that work as a single unit during compilation, especifically at the linking stage. During the linking stage libraries supply an inventory of definitions which are linked to the execuatble file. In a Linux system you can find the standard library files in /lib/ and /usr/lib.

In the physical world you can have your own library or you could use a shared library such as pubic library, college library and so on. Likewise, in C you have two types of libraries

  • Static libraries
  • Dynamic libraries, otherwise known as shared libraries

The biggest difference is that static libraries are linked to the executable file during the linking stage of the compilation process while shared libraries are linked to the execitable file as the program is executed. That way an executable linked with a static library will contain the physical contents of the static library.

The perks of using a static library

As your program grows in size so will compilation time increase. At the same time if your program is filled with tons of different funtions the program will be less productive and more puzzling to other programers. There is where libraries come to the rescue making your program more portable. By indexing definitions and symbols located in the library compilation time will be reduced since the compilator will only need to search within a few files and not the entire hard disk. This also avoids duplication so that the same code does not have to be rewritten over and over again.

In relation to shared libraries, programs linked to static libraries are faster since the machine code is physically available during the linking stage. However, file size will be much larger due to the same reason.

How to use static libraries

Enough chit-chat, let’s get our hands dirty. Libraries contain a collection of ojects files with the extension .o combined into one file of extension .a.

As you can see below all the files we want to include in the library are listed within the same directory. In order to do so we must compile these files but only up to the assembler stage. We can do so by adding the -c option to our compilation. Therefore, our .c files will be turned into .o files.

As you can see below, all of our .c files were turned into .o files

At this point we can archive these object files into a static library. In order to create a static library we must use the ar command which will take all the .o files and add them to an .a file thus creating our static library. Below you can see an example,

ar -rc staticlibrary.a file.o

  • the -r flag will update older files within the library
  • the -c flag will istruct ar to create the library if it doesn’t exist
  • staticlibrary is the name of the library to be created
  • file.o is the name of evert object file to be included in the static library

After creating the library you should index it. Some archivers will index it for you but better safe than sorry so it is advisable to run the command just in case. The command used in order to create the index is ranlib,

ranlib staticlibrary.a

That’s very nice and all but…how can I use my brand new library?

At this point the library can be used by any program. In order to do so you must use the below command,

gcc hello.c -L. -lstaticlibrary -o hello

  • hello.c is the program we want to compile
  • -L instructs the compiler to search for library files within this directory
  • -l(library name)
  • -o hello where “hello” is the output file name

Should you wish to check the contents of your library file you could use the nm command.

So that’s about it. I hope I have awaken your curiosity about libraries. Thanks for your attention!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store