Why use libraries
Programmers use libraries so we don’t have to write common functions over and over again in our programs (e.g. Instead of coding a function to print something out, C programmers use the printf function and include its library by writing #include <stdio.h> in the program). We can even create a custom library with only the necessary functions we need. There are two ways to do so in Unix and other modern systems: Static Libraries and Shared/Dynamic Libraries. Last time, I talked about how to create and use Static Libraries. This time, let’s look at how to create and use Dynamic Libraries.
How they work
While static libraries are not relevant during runtime and are only linked towards the end of the compilation process, dynamic libraries are loaded into memory and only accessed during runtime. While static libraries insert a copy of it’s entire code into your program, dynamic libraries allow for addresses of specific called functions to be accessed.
How to create a Dynamic Library? (Linux, C)
gcc -c -fPIC *.c
Compile all .c files holding your custom functions up to the linking stage with the flag -fPIC to generate Position Independent Code.
gcc -shared -o libmelissaslib.so *.o
Instead of using ar to create an archive like you would for static libraries, create a dynamic library by finishing the compilation: gcc with the -shared -o flags. Write the name of your custom library with the prefix lib and Linux extension .so (shared object.) (Windows is .dll) Now, include all your .o object files by utilizing the wildcard *.
nm -D libmelissaslib.so
Use nm if you want to see what functions your library holds. The -D flag refers to the symbols in the initialized data section.
It is important to update your environmental variable LD_LIBRARY_PATH to include the directory to where your custom library can be found. (in the current directory as signified by `.` in this case) If you want to check if your custom library is found by the loader, you can use “ldd.”
How to use your dynamic library
gcc -L. -o your_program_name.c main.c -lmelissaslib
Time to use the library you’ve custom created! It contains all the necessary resources your program needs to run. Compile your_program_name.c by including the path to your static library -L. (in the current directory as signified by `.` in this case), your executable program’s new name, your library’s name with the -l flag and without the prefix lib and the suffix .so. Your executable program should now be able to run with “./”
Differences, Advantages, and Disadvantages
An advantage of dynamic libraries over static libraries is the use of less memory space. Another advantage of dynamic libraries is not having to recompile the main program after a dynamic library is updated, whereas you would have to recompile a program if the static library updated. Finally, a disadvantage of dynamic libraries is that the library has to be present for your program to run, so it makes your program less portable than a program that was linked with a static library. That’s it!