Creating a static library
In Chapter 3, Diving Deep into C in Linux, we saw how to create a dynamic library and how it was linked from the current working directory. In this recipe, we'll make a static library instead.
A static library is included in the binary during compilation. The advantage is that the binary gets a bit more portable and independent. We can remove the static library after compilation, and the program will still work.
The downsides are that the binary will be slightly larger and that we can't update the library after it has been compiled into the program.
Knowing how to create static libraries will make it much easier to distribute and reuse your functions in new programs.
For this recipe, we'll need the GCC compiler. We will also use a tool called
ar in this recipe. The
ar program is almost always installed by default.