Static vs Shared Library
Why use libraries?
In programming, a library holds functions that are compiled and ready for use. They are stored in one place for accessibility. The functions could be used and reused saving time from having to rewrite them again.
How do they work?
There are two type of libraries: static and shared (or dynamic). Both are an executable made from a collection of object files. There are a few steps taken before they become an executable. First, at the pre-processor stage the program code processes constants and macros. Next, at the compilation stage the source files are converted into object files that are machine readable. Then, at the linking stage the .o files and other shared libraries that already in the operating system are linked. Finally, at the loading stage the libraries are mapped into the program for use.
In my previous article, I wrote extensively on static library. Please check it out at the link below on how to create and use a static library.
https://www.garudax.id/pulse/build-your-own-static-library-jennifer-tang
How to create them?
First, create a header file that defines all the function names like so:
Next, we need to use gcc -fPIC -c *.c to compile for Position Independent Code for all the source files ending with a .c extension. It also generates relative addresses for jump calls and subroutine calls as to not use absolute addresses like in a static library. Then, use gcc -fPIC -shared *.o -o libname.so to create the shared library.
When creating a dynamic library it's obviously not already in the system. We need to use the environment variable, $LD_LIBRARY_PATH to look into other directories for the newly created dynamic library. You can export the library into the variable by LD_LIBRARY_PATH=/full/path/to/library/directory and then, export=LD_LIBRARY_PATH.
In order to check whether the library was created and stored in the variable, use ldd executable. Side note: the ldd config command creates the links and cache to other shared libraries.
The nm -D liball.so command would show a list of all the symbols used in the object file.
The difference from creating a static library is the command ar is used to create and ranlib is used to show the list of all the indexes used in the object file.
How to use them?
Use the command gcc -L main.c -lall -o a when you compile your main function file along with the shared library. The -L option will have the linker look for the shared library file in the current directory.
What are their differences?
A dynamic library is also known as a shared library. Like a static library, it collects and combines multiple object files to create one executable. The difference is during linking which is the last step in compilation of a program code. A static library takes all the object files and copies them permanently into the executable before runtime. Meanwhile, during linking of a dynamic library, it doesn't permanently copy object files into the executable. It will scan for the object files and link them at runtime.
What are their advantages and disadvantages?
Resources
https://www.cprogramming.com/tutorial/shared-libraries-linux-gcc.html
http://docencia.ac.upc.edu/FIB/USO/Bibliografia/unix-c-libraries.html#what_is_a_library
https://cs-fundamentals.com/c-programming/static-and-dynamic-linking-in-c.php