How to use static libraries in C
Today I want to talk about a topic that can save you a lot of time and that is libraries. There are two types of libraries in C: Static and Dynamic, in this article, we are going to talk mainly about Static libraries. Creating a library or using an existing library is very useful as it allows you to reuse your code or to use functions created by others without having to rewrite the code. Static libraries are simply a collection of ordinary object files (if you don't remember what is an object file, please refer to my last post about compiling process in c).
Static libraries, unlike dynamics, gather all objects files into one executable file, while dynamics exist as separate files outside the executable. It is called a static library because the library will remain unchanged unless the program is recompiled. When using a dynamic library, the programmer is referencing that library when it needs to at runtime, this is possible thanks to the dynamic loader (we will discuss more this topic in future articles).
The benefit of using a static library is that the functions and other symbols loaded into it are indexed. This makes the process of searching for a file faster than if the compiler had to check for every object file in separate parts of the computer.
Now we are going to learn through an example how to create and how to use static libraries:
1- First you need the c programs and the header with the prototypes of your functions that you want to have in your library, like in the following example:
Remember to use the macros #ifndef and #define at the top of your header file and #endif at the bottom of your header file, in this example it is inside the "main.h" file. We use these macros so that the header file is only defined once instead of each time it is called, if you do not use it you could have some problems when compiling.
2- The next step is to generate the object code of the c programs, this can be done with the following line of code (we are going to use the wildcard * to select all the .c files):
gcc -c *.c
As you can see all the .o (object files) have been successfully generated.
3- Now we need to create an archive with the "ar" Linux command that allows you to create a single file holding a collection of other files?in a structure that makes it possible to retrieve the original individual files.
Use this code:
ar -rc myfirstlib.a *.o
The c flag tells ar to create the library if it doesn't already exist. The r flag tells it to replace older object files in the library, with the new object files.
In yellow, there is our library: myfirstlib.a
领英推荐
4- Next we could use the command ranlib to generate an index to the contents of an archive and store it in the archive.? This is useful as we stated before to save us time because an archive with an index speeds up linking (the last step of the compilation process) to the library and allows routines inside the library to call each other without regard to their placement in the archive.
This step may or may not be necessary depending on your computer system or your archiver it is not necessary if you use the ar command. If we want to see the contents of our library, we can use the?ar?option?-t.
ar -t myfirstlib.a
We can also use the command: nm to see the symbols in our library, which lists each symbol’s value, type, and name from object files.
Congratulations we have created our first static library using C, now let's see how to use it:
If we are using the gcc compiler we need to use the following flags so we can use the library we created with a program:
gcc main.c -L. -lmyfirstlib -o quote
Finally, we can run the executable program 'quote' program with the following command: ./quote
I had created a main.c program with the following quote:
Main advantages of static libraries:
Main disadvantages of static libraries:
Thanks for reading I hope this was useful! Until next time.