Static libraries in C
In simple words, a static library is a collection of object files containing the functions you have created and need to use, that you can call from your programs. It can be a completed executable or intermediate file, that is used during the linking phase of compilation along with a .h header file, which contain the declarations of the objects defined in the library. During this phase the linker makes a copy of all used library functions to the executable file. Static Library files have the prefix lib and the suffix .a in Linux.
Why use static libraries?
Libraries are very important in C language because it supports only the most basic features that it needs. For example, C doesn’t contain input-output (I/O) functions to read from the keyboard and write to the screen. Anything that extends beyond the basics must be written by a programmer. If a piece of code is useful to multiple different programs, it’s often put into a library to make it easily reusable.
You can write C libraries. By doing so, you can divide your program into reusable modules. This modular approach not only makes it easy to include the same code in multiple programs, but it also makes for shorter program files which are easier to read, test and debug. Keep reading and you’ll be the creator of your own static library.
Creating a static library
1.- Create the .c source files containing the functions you want to use, or if you already did this, move them to the directory where your library is going to be created.
2.- Create a header .h file with the prototypes of the functions and include it in your .c source files.
3.- Compile your source files without linking them, it means compile them into object files. In order to do this, use the -c
flag, using GCC compiler the command line is: $ ggc -c *.c
4.- Once you have the object files, it’s time to create the .a library file. To do this use the ar
command, is a Unix utility that maintains groups of files as a single archive file. And also you need to use -r and -c flags, the first one says to replace older object files and the second says to create the library if it doesn’t already exist.
The command line should be similiar as shown: $ ar -rc libnew.a *.o
If you want to see the contents of your library, you can use the ar flag -t.
$ ar -t libnew.a
5.- Index the library using ranlib, this makes a header in the library with the symbols of the object file contents which helps the compiler to quickly reference symbols.
$ ranlib libnew.a
You can also see the symbols in your library, using the command nm
, which lists each symbol’s symbol value, symbol type, and symbol name from object files.
$ nm libnew.a
Congrats! Now you have created your first library. Let’s see how to use it.
Using a static library
The next step is invoking the library as part of the compilation and linking process when creating an executable program. In case of GCC, use the following flags:
- -l, this flag adds the prefix and the extension that’s why you mustn’t write them.
- -L, specifies the path to the library .We can use it in order to point to the current directory and -L/home/tmp to point to the /home/tmp directory.
$ gcc mymain.c -L. -lnew -o mymain
Here’s a disadvantage in the static linking process: every time you want to make any logical changes to your functions inside your library in your C files, you have to go through all the steps listed above to reuse your static library. So, the shared libraries were created… But that is for another time.
Until then, happy learning!