Using Static & Dynamic Libraries
A look into what they are, how they work & how to make and use them.
What are they?
Programming libraries are a collection of implementations of behavior. Whether it be small or large, libraries help contribute to a program’s efficiency through a set of well-defined interfaces to invoke the desired output. Some libraries are provided (see stdlib, stdio, etc), but anyone can make and implement a library however they see fit. This can come in handy when building out similar programs. If most of the functionality overlaps, that library can be implemented on another program.
How do they work?
Upon compile time, the contents are resolved and copied into a target application. Using either the compiler, linker or binder, this creates an object file and a stand-alone executable. Beyond just the broad strokes of holding information, libraries have a bit more to them. One way they differ is between being categorized as static or dynamic. Static, or statically-linked libraries and dynamic, or shared libraries are the same in their contents, but the difference comes from where they can be used. While static libraries can be organized and centrally located for a certain project, that same library would not be available to a program outside its grasp. However, dynamic libraries would have no problem with this since their location isn’t contingent on its functionality within a program.
How to make and use them?
Once the desired contents are organized and the library is ready to be made, gcc or the GNU Compiler Collection and the argument -c comes into play. Implementing ‘gcc -c’ will compile or assemble the source file, but not link. With this action, a static library is created and the library is available in the immediate working directory. To create a dynamic library, the -fPIC argument is added. This stands for Position-Independent Code, which means code will execute regardless of its absolute address. Once the library is declared independent, the library must be brought into the PATH using the command LD_LIBRARY_PATH as shown below. This will ensure the library is available throughout the entire scope that is handled. Both styles of libraries have their advantages, dynamic being its ability to conserve memory and repeat the use of the library within many programs, and static being the guarantee that all the desired behavior is within immediate grasp of the program. Determining the type of library created can be very important, whether it be as simple as wasting the potential of a dynamic library and only using it for one program or having to recompile the library at a later date.
Whether a standard library is used, or a self made collection of code, understanding the dialogue that goes on inside the machine is incredibly important. Without understanding why everything is organized the way it is, the true potential of a command can be lost in a library of information.
— — Written by Kathleen R McKiernan for Holberton School NHV — —