C Static Libraries: Understanding what they are/do.
https://www.nema.org/images/default-source/default-library/home-1.jpg?sfvrsn=742bd6_0

C Static Libraries: Understanding what they are/do.

TL;DR: “A library is a file containing several object files, that can be used as a single entity in a linking phase of a program.”

Static libraries take their name (Static) from the fact that the library will remain unchanged unless the program is recompiled.

Why use libraries?

Let’s say you are normally writing your code which can get extensive and occupy more and more space also causing the compilation phase to last longer than usual. Now as a programmer you might be working on a tight schedule and efficiency is a determining factor to having projects completed on time. By including libraries, you are telling the computer to get more organized with your code (libraries normally are indexed). With this you are keeping your object files in one place and therefore minimizing the number of files that need to be searched and opened. Now in contrast to dynamic libraries, which are shared libraries, a static library is linked to the program.

How they work?

No alt text provided for this image

Normally when you are working on a program to have said code work you would need to compile the program. Taking into reference one of my previous blog posts that you can read about here, the compilation process can be divided into four sections: Preprocessing, Compilation, Assembly and Linking. During this last step is where static libraries come to play. To better understand it we must first cover the fact that object files are basically the output from the compilation phase. Being mostly machine code, it contains certain information allowing a linker to see what symbols it contains and symbols it will require to work. (Symbols refers to names of global objects, functions, etc.) Static libraries are a collection of these object files that during the linking stage of compilation are included or linked into your program in order to form a final executable file of your program.

No alt text provided for this image

How to create them?

First things first. Before going on ahead and creating a static library we should first determine what is the code we want to include in them. In order to have that all set we need to compile the code(s) into object files (from .c extension to .o extension). In order to do this we run the gcc compiler with the option -c which compiles and assembles the code but it doesn't link it yet leaving you with the object files (machine code pending to be executable).

$ gcc -c *.c /* Compile code to include in static library into object files. */

Now that we got that out of the way we can go ahead and start making the static library. For this we se the ar (or archiver) with the options -rc whereas c stands for creating the archive file if non-existent and r to replace previous versions of files if needed. Lastly we include *.o indicating to select the object files that were created previously.

$ ar -rc libExample.a *.o

Although we have the archive created lets go ahead and add some indexing to it. By adding a index to it we are aiding the compiler to lookup symbols within the library, make sure the order of the symbols won't matter and thus speeding up the process (may not be needed depending on system). To do so we use the command ranlib.

$ ranlib libExample.a

How to use them?

Before going on to how to use them while compiling to obtain your executable machine code here are some honorable mentions for uses that are good to know when working with a static library:

$ ar -t libExample.a /* List contents of library */

$ cp -p libExample.a /* Copy (& keep all attributes) file to another location */

Onward to using the static library in the compilation of your code we can see below the options -L and -l:

  • -L is used to indicate the linker that libraries might be found in the current directory additional to any standard location the compiler looks for libraries. (We can use the dot "." to indicate current directory.)
  • -l is used to ommit the prefic "lib" as well as the ".a" extension from the name.
$ gcc main.c -L. -lExample -o main

Now that we ran the compiler with the static library we get as a result a output file that contains the executable machine code used to run your program. This may seem a bit confusing and there are many arguments out there about how there are pros and cons to it such as needing to compile your code again and again if a change is to be done to the static library. Not to mention the fact that we haven't mentioned shared or dynamic libraries but that will be a topic for another day. For now I hope you enjoyed this small reading, I take no credit for the images and Joyful Coding :)

要查看或添加评论,请登录

Xavier E. Figueroa Muniz的更多文章

社区洞察

其他会员也浏览了