Static Vs Shared Libraries
Static Libraries vs. Dynamic Libraries: These are pre-compiled object code which is used in source code file to provide functionality for a pre-defined function or user-defined functions. There are two types of libraries widely used in Linux domain static libraries and shared libraries.
Shared Libraries: When function routines or function definitions are placed in the source code file, during compilation linker binds the function definition with source code to create expanded object code. Usually, such libraries are large in size and faster in execution. If any changes are needed then modify the actual source file & recompile it.
Functions are blocks of code that are reusable throughout a program. Using them saves time, removing the need to rewrite code multiple times. Libraries, like functions, also save time in that they make functions reusable in multiple programs.
Static libraries, while reusable in multiple programs, are locked into a program at compile time. Dynamic, or shared libraries, on the other hand, exist as separate files outside of the executable file.
The downside of using a static library is that its code is locked into the final executable file and cannot be modified without a re-compile. In contrast, a dynamic library can be modified without a need to re-compile.
Because dynamic libraries live outside of the executable file, the program need only make one copy of the library’s files at compile-time. Whereas using a static library means every file in your program must have its own copy of the library’s files at compile-time.
The downside of using a dynamic library is that a program is much more susceptible to breaking. If a dynamic library, for example, becomes corrupt, the executable file may no longer work. A static library, however, is untouchable because it lives inside the executable file.
The upside of using a dynamic library is that multiple running applications can use the same library without the need for each to have its own copy.
Another benefit of using static libraries is execution speed at run-time. Because it’s object code (binary) is already included in the executable file, multiple calls to functions can be handled much more quickly than a dynamic library’s code, which needs to be called from files outside of the executable.
What does this mean in practical terms? Well, imagine you’re a developer who has issued an application to thousands of users. When you want to make a few updates to the app, would you rather have to re-issue the entire program, or would you rather just issue updates in the form of modified libraries? The answer depends on the downsides your application can afford.
If you have a lot of files, multiple copies of a static library mean an increase in the executable file’s size. If, however, the benefits of execution time outweigh the need to save space, the static library is the way to go.
Steps: 1. Compile optional .c files to create an object file.
$ gcc –c add.c $ gcc –c sub.c
- Link both object files to get static library
$ar –rs lib.a add.o sub.o
- Create object file for source code file
$ gcc –c main.c
- Link static library with main.o to create executable file
$gcc lib.a main.o => ./a.out
Dynamic Libraries: When function definitions are placed in a separate file, during compilation these files are compiled as position-independent code. So, the linker binds these libraries in such a way they are not pre-loaded into an object file. But they are loaded at run time from an independent address different from the object file address.
Steps: 1. Compile optional .c files as position independent code.
$gcc –Wall –fPIC –c add.c
$gcc –Wall –fPIC –c sub.c
- Create shared libraries using add.o, sub.o
$gcc –shared –o lib.so add.o sub.o =>lib.so
- Create object file for main program
$gcc –c main.c
- Link shared library with object code of main
$gcc –o tr main.o lib.so
- Move shared library (.so) to standard path /usr/lib/
$mv lib.so /usr/lib/
- Run “ldconfig” to link & load dynamic library from standard path
- Now check dynamic library is linked using ldd
- Now execute the program
- Amit Kumar