Brief: This article guide clarifies the differences between static and dynamic libraries in relation to their applicability in a Linux operating system environment.
Static libraries and dynamic libraries comprise a group of compiled object files. An object file results from the compilation of a C program.
The object files are used for multiple programs. Static and dynamic libraries share a common purpose, which is to provide reusable code and data that are shareable with different programs.
Differences Between Static and Dynamic Libraries
The following factors distinguish static libraries from dynamic libraries:
Program Linkup Time
Static libraries need to wait for compile time before linking with a program. Dynamic libraries need to wait for runtime before linking with a program.
In short, a program’s executable file accommodates static libraries during its execution while dynamic libraries need to be loaded into memory first before a running program can access it.
Size
Before static libraries include all code and data needed by a program executable, they tend to be larger in size. Dynamic libraries are lightweight because they are only associated with needed code and data references required by the program executable.
Portability
The existence of static libraries is independent of the availability of specific libraries within the system that hosts them hence their portability attribute.
Dynamic libraries’ usability depends on the presence of other libraries within the system that hosts them making it impossible to move them from one system to another.
Efficiency
Static libraries are only loaded into memory once and are instantaneously accessible and shareable to multiple programs making them more efficient. Every time a program needs to use/access dynamic libraries, it must be loaded into memory. This approach slows the program execution time and makes dynamic libraries less efficient.
Usability Criteria
Static libraries are ideal for distributable programs that can execute/run in different operating system environments. On the other hand, the ideal environment for dynamic libraries is system-specific OS environments that don’t need distributable programs/software.
Creation of Libraries
The ar command is used to create static libraries, while clang or gcc compilers are vital in the creation process of dynamic libraries.
For instance, the creative process behind the existence of a static library called libexample.a
will require one or more object files e.g file1.o, file2.o, and file3.o.
The command behind the creation of this static library from these object files will look like the following:
$ ar -rcs libexample.a file1.o file2.o file3.o
The explanation of the command is as follows:
-ar
is the Linux ar command.-r
adds the object files file1.o, file2.o, and file3.o to a static library (libexample.a).-c
creates a static library (libexample.a) if it does not exist.-s
uses a symbol table to update the static library (libexample.a).
On the other hand, the command behind the creation of a dynamic library called libexample.so
from these same object files will look like the following:
$ gcc -shared -o libexample.so file1.o file2.o file3.o
The explanation of the command is as follows:
-gcc
is a compiler program (used to compile and link C programs).-shared
invokes the creation of a dynamic library (libexample.so) from object files (file1.o, file2.o, file3.o).-o
specifies the filename for the dynamic library (libexample.so).
File Extension
Static libraries are associated with a '.a'
file extension (for example, libexample.a
). On the other hand, dynamic libraries are associated with a '.so'
file extension (for example, libexample.so
).
Program Linkage
The -l
and -L
command options are used to link static libraries to a program via clang or gcc compilers.
For instance, to link a program called the driver to a static library called libexample.a
, we will implement the command:
$ gcc -o driver driver.c -L/path/to/lib -lexample
The compiler will first trace libexample.a
static library in the directory path /path/to/lib before linking it to the driver (executable program).
With dynamic libraries, the -l
, -L
, and -rpath
options are used to link these libraries to a program. For instance, the following command links the driver program to the dynamic library libexample.so
.
$ gcc -o driver driver.c -L/path/to/lib -lexample -Wl,-rpath=/path/to/lib
The gcc compiler searches the directory /path/to/lib for the dynamic library libexample.so
before linking it to the driver (program executable).
The rpath is the assumed location (by the dynamic linker) of the dynamic library (libexample.so
) at runtime. The dynamic linker is responsible for connecting the program executable with the dynamic library functions code.
Program Accessibility
Multiple programs can access static libraries at the same time while with dynamic libraries it is one program at a time. With static libraries, the programs’ executable files are preloaded with the static library data.
With dynamic libraries, the library data is preloaded into memory at runtime and therefore creating a first-come-first-serve routine among programs that want to use the library. However, only concurrently executing programs have the privilege of sharing the same dynamic library.
Despite the advantages, Dynamic or Static libraries might have over each other, program libraries are irreplaceable in their code and data-sharing techniques among multiple programs.
This functionality significantly reduces code duplication in favor of improved program maintainability. Program libraries are also ideal for implementing functionalities absent in standard programming language libraries.