Creating and Consuming Static Library in C
Hello again guys, in this article we will take a look at Static Library in C. I will first explain what it is, then we take a look at how to create it and how to consume it. so lets go, shall we?
Static library or statically-linked library is a set of routines, external functions and variables which are resolved in a caller at compile-time and copied into a target application by a compiler, linker, or binder, producing an object file and a stand-alone executable. This executable and the process of compiling it are both known as a static build of the program. Historically, libraries could only be static. Static libraries are either merged with other static libraries and object files during building/linking to form a single executable, or they may be loaded at run-time into the address space of the loaded executable at a static memory offset determined at compile-time/link-time.
What is a Static Library
A static library is just an archive containing object files. Object files provide definitions just like header files provide declarations. This static library is then later supplied to the linker during compilation process. That is, during the compilation process, the static library supplies the compiler with its content, which are the object files, during the linking stage of a C compilation process. A static library is basically a collection of object files, kind of like a .zip file but probably not compressed as in .zip archive. By convention, static libraries have the prefix lib and the suffix .a
Why use Static Libraries?
Why use static libraries you may wonder, especially when one could just gcc away and run your resulting program straight and away. Afterall, the program would work same way as when using libraries. Well, the reason to use libraries are not much but are definitely advantageous. For me personally, the one of the main reasons is the fact that using libraries make my program portable. With ever changing real world needs, our applications are expected to work every time, everywhere and anywhere. This is where these use of libraries come in; these libraries help your program to do the same thing on every computer. Another reason to use them is because they save considerable development time. They also make your work organized, especially if you have a very large project with lots of files in the working directory. Since they are just like normal archive, they keep your object files neatly packed in an archive and the archive is supplied to the compiler during the linking stage.
How Static Libraries Work
A static library is purely a collection of .o files, put together in an archive just like a zip archive but unlike a zip archive, with no compression. It is now ready for linking, the linker during compilation will search the library for .o files that provide any of the missing symbols in the main program, and pull in those .o files for linking, as if they had been included on the command line like .o files in your main program. This process is applied recursively, so if any of the .o files pulled in from the library have unresolved symbols, the library is searched again for other .o files that provide the definitions. Static libraries run at compile time and actually embed inside the code.
How to create a Static Library
To create a static library using GCC we need to compile our library code into an object file so we tell GCC to do this using -c
$ gcc -c *.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
The archiver, also known simply as ar, is a Unix utility that maintains groups of files as a single archive file.
$ ar -rc libholberton.a *.o
This tells ar to create an archive (option c) and to insert the objects, replacing older files where needed (option r). Whenever files are added to a library, including the initial creation of the library , the library needs to be indexed, which is done with the command ranlib. ranlib makes a header in the library with the symbols of the object file contents.This helps the compiler to quickly reference symbols. A large library may have thousands of symbols meaning an index can significantly speed up finding references.
$ ranlib libholberton.a
This step may or may not be necessary depending on your computer system or your archiver(not necessary with ar). If we want to see the contents of our library, we can use the ar option -t.
ar -t libholberton.a
We can also see the symbols in our library, using the command nm, which lists each symbolís symbol value, symbol type, and symbol name from object files.
How to consume a Static Library
We have now created a static library libholberton.a and now let us use the static library by invoking it as part of the compilation and linking process when creating a program executable. Incase of gcc we use following flags to create static library
- -l : libraryname without lib prefix and extension
- -L : specifies the path to the library .We can use -L. inorder to point to the current directory and -L/home/tmp to point to the /home/tmp directory.
gcc main.c -L. -lholberton -o main
Now run the executable program 'main'
Executable generated using static libraries are no different than executable generated from individual source or object files. Static libraries are not required at run-time, so you do not need to include them when you distribute your executable. At compile time, linking to a static library is generally faster than linking to individual source files.
Thats all for now folks. Enjoy coding and watch out for my next article.