Everything you want to know about C static libraries
“Without libraries what have we? We have no past and no future”
Hello, In this blog, we’ll be talking about what a library is and how they are useful. How they work? How to create them? Thank you for reading and happy coding! So, let’s get started!!
What’s a library?
A library is a collection of objects that are made available for use by other programs. These objects are generated during the compilation process of your program just before the linking step, and are composed of object code, which is pretty close to machine code but hasn’t yet been completely added to the final executable of the program. The extension of these files is usually “.o”.
Types of libraries
Basiclly, we have two types of libraries: static libraries and dynamic libraries. In this post will be discussing static library.
What’s is a static library?
A static library (also known as an archive) consists of routines that are compiled and linked directly into your program. When you compile a program that uses a static library, all the functionality of the static library that your program uses becomes part of your executable. Static libraries use the extension a the a stands for archive.
How it is useful?
Static libraries are guaranteed to be present in the app and have correct version. No need to keep an app up to date with library updates. Better performance of library calls. libraries can help us to save considerable development time. especially with larger and more complicated programs as it reduces chances for errors and can create a more readable program.
How do libraries work?
Creating a Static Library file
First thing you must do is create your C source files containing any functions that will be used. Your library can contain multiple object files.
After creating the C source files, compile the files into object files. so we tell GCC to do this using -c
Here in the above command , all the .c extension files( C files) in the current working directory have been converted in to their respective object files. Once we have object file(s), we use the GNU ar command to create our final library/archive.
To create a library
his will create a static library called libholberton.a. Rename the “holberton” portion of the library to whatever you want. This tells ar to create an archive (option c) and to insert the objects, replacing older files where needed (option r).
There are two ways to create or update the index. The first one is, by using the command ranlib.
$ ranlib liholberton.a
or by adding an extra flag (-s) to the ar command and it becomes like this:
$ ar -rcs liholberton.a *.o
How to use them?
I will show you an example that illustrate how we can use static libraries:
Let us take two c files, max.c, and multiply.c which make respectively the maximum and the multiplication of two integers, and a header file named ‘holberton.h’ that contains the prototypes of these functions. The picture below shows the output generated after using the command ls.
Let’s now create a static library
The picture below shows the execution of these commands on our example
In order to list the names of the object files in our library, we can use the ar command with -t flag.
Now our static library “libholberton.a” is ready to be used. we can use it in a program. we create a C source file named main.c
Now we will create our final executable program by using this command:
$ gcc main.c -L. -lholberton -o main
-L : Specifies the path to the given libraries (‘.’ referring to the current directory)
-l : Specifies the library name without the “lib” prefix and the “.a” suffix.
now we will just run our program.
Libraries are files that define pieces of code and data that are not a part of your Xcode target. It obviously has a lot of advantages.
Thanks for reading!