Shared (dynamic) Libraries vs. Static Libraries
A library is a file containing various object files. Those object files are the results of the 3rd phase of the compilation process, which containing 4 phases : preprocessing, compiling, assembling and linking. These are files ending with .o, containing binary code but not directly executable.
Static and Dynamic libraries will differ in the linking phase : the first one will be used with Static linking while the latter will be dynamically linked. Usually, static library are file ending with .a (…archive), while dynamic library are ending with .so (… shared objects)
Static linking means that library becomes part of the executable code, the full code of the library is inserted in the final compiled file.
Dynamic linking means that the library routines are loaded dynamically during execution : the dynamic libraries are also known as shared libraries.
Static vs Dynamic : Pros and Cons
- Use of resources
Dynamic libraries allow several programs to share the same code so that they don’t need to have individual copies of the code. This saves storage and memory space during execution. Static libraries waste both storage and memory because every program has to have its own copy of the library, therefore its inefficient.
2. Updating programs
Dynamic linking makes updating programs super simple and resource-efficient. When you update a program, you must download the new dynamic library and remove the old one. Static linking makes updating programs a chore and wastes resources. Whenever a library is updated, the whole program would have to be relinked, which results in a bulky executable.
3. Programming language compatibility
Dynamic libraries can be shared among programs of various programming languages. Static libraries are not shared, therefore they do not have this feature.
Dynamic libraries are not integrated into the programs that use them, which means that programs that require these libraries are not self-sufficient. In other words, a program that requires a dynamic library would not function if that dynamic library is missing. For static libraries, any change made in the library of one program would not impact other programs because they are independent.
5. Unauthorized use
Because dynamic libraries are shareable, it can invoke unauthorized library sharing. Static libraries, on the other hand, have no risk of unauthorized use because they are contained inside a program and cannot be shared.
Dynamic libraries exist outside of programs, therefore they can be modified easily. This potentially means that hackers can tamper with the modules that ensure software protection. This problem is non-existent in static libraries.
In general, programs that use dynamic libraries are slower than programs that use static libraries
How to create a static library ?
To create a static library, or to add additional object files to an existing static library, use the program ‘ar’, which stands for ‘archive’. This command can be used as well to list the name of object diles in the library.
After having create object files of the various functions you want to include, using gcc -c command (the -c option will stop the compiling process just before the linking phase and produce .o machine code files), you can just type the following command :
This will create a library called libtest.a from all the .o files that are in the current folder. The ‘.a’ extension still stands for ‘archive’. The option -r stands for “Insert the files member… into archive (with replacement).”. The option -c stands for “The specified archive is always created if it did not exist, when you request an update”
If needed use the ‘ranlib <libtest.a> to index the library, or add -s option when running the previous ar command line
How to use your static library
As seen previously, you can use a static library by invoking it as part of the compilation and linking process when creating a program executable.
Using gcc compilation program, you just need to use the special
-l flag, leave out the prefix and
.a suffix. The linker attach these parts back to the name to create a name of a file to look for. You need also to use the
-L to precise the library location.
This command will create an executable called ‘main’, from the main.c file, using as well the library ‘libtest.a’ functions.
How to create a dynamic library ?
To create a dynamic library, we need to compile the .c function, using the -c arguments (to stop the compiling just before the linking) and we need to add the flag -fPIC which stands for “position independent code”, which is a requirement for shared libraries. This allows the code to be located at any virtual address at runtime.
To create the library file, we use the following command :
This will create a library called libcreated.so from all the .o files that are in the current folder. The option -shared indicated to gcc that we are compiling a shared library.
How to use your dynamic library
The first step to use a dynamic library is to indicate to the compiler where to look for the .so file : for example, if, the newly create library is in your current directory, just add it to the environment variable LD_LIBRARY_PATH
Then, we can process with the gcc compilation program, using the following syntax: as for Static linking, you need also to use the
-L to precise the library location.
This command will create an executable called ‘test’, from the main.c file, using as well the library ‘libcreated.so’ functions.
To check that the library has been properly included, you can use ldd command :