Static vs Dynamic libraries

Static Linking and Static Libraries:

====================================

Static Linking and Static Libraries is the result of the linker making copy of all used library functions to the executable file. Static Linking creates larger binary files, and need more space on disk and main memory. Examples of static libraries (libraries which are statically linked) are, .a files in Linux and .lib files in Windows.

Compile library files.

 gcc -c lib_mylib.c -o lib_mylib.o

Create static library. This step is to bundle multiple object files in one static library (see ar for details). The output of this step is static library.

 ar rcs lib_mylib.a lib_mylib.o 

Following are some important points about static libraries.
1. For a static library, the actual code is extracted from the library by the linker and used to build the final executable at the point you compile/build your application.

2. Each process gets its own copy of the code and data. Where as in case of dynamic libraries it is only code shared, data is specific to each process. For static libraries memory footprints are larger. For example, if all the window system tools were statically linked, several tens of megabytes of RAM would be wasted for a typical user, and the user would be slowed down by a lot of paging.

3. Since library code is connected at compile time, the final executable has no dependencies on the the library at run time i.e. no additional run-time loading costs, it means that you don’t need to carry along a copy of the library that is being used and you have everything under your control and there is no dependency.

4. In static libraries, once everything is bundled into your application, you don’t have to worry that the client will have the right library (and version) available on their system.

5. One drawback of static libraries is, for any change(up-gradation) in the static libraries, you have to recompile the main program every time.

6. One major advantage of static libraries being preferred even now “is speed”. There will be no dynamic querying of symbols in static libraries. Many production line software use static libraries even today.

 

 

Dynamic linking and Dynamic Libraries:

====================================

Dynamic linking and Dynamic Libraries Dynamic Linking doesn’t require the code to be copied, it is done by just placing name of the library in the binary file. The actual linking happens when the program is run, when both the binary file and the library are in memory. Examples of Dynamic libraries (libraries which are linked at run-time) are, .so in Linux and .dll in Windows.

[root@host ~]# gcc -Wall -fPIC -c add.c 
[root@host ~]# gcc -Wall -fPIC -c sub.c

[root@host ~]# gcc -shared -o libheymath.so add.o sub.o

 

Static Linking Dynamic Linking
Static linking is the process of copying all library modules used in the program into the final executable image. This is performed by the linker and it is done as the last step of the compilation process. The linker combines library routines with the program code in order to resolve external references, and to generate an executable image suitable for loading into memory. When the program is loaded, the operating system places into memory a single file that contains the executable code and data. This statically linked file includes both the calling program and the called program. In dynamic linking the names of the external libraries (shared libraries) are placed in the final executable file while the actual linking takes place at run time when both executable file and libraries are placed in the memory. Dynamic linking lets several programs use a single copy of an executable module.
Static linking is performed by programs called linkers as the last step in compiling a program. Linkers are also called link editors. Dynamic linking is performed at run time by the operating system.
Statically linked files are significantly larger in size because external programs are built into the executable files. In dynamic linking only one copy of shared library is kept in memory. This significantly reduces the size of executable programs, thereby saving memory and disk space.
In static linking if any of the external programs has changed then they have to be recompiled and re-linked again else the changes won’t reflect in existing executable file. In dynamic linking this is not the case and individual shared modules can be updated and recompiled. This is one of the greatest advantages dynamic linking offers.
Statically linked program takes constant load time every time it is loaded into the memory for execution. In dynamic linking load time might be reduced if the shared library code is already present in memory.
Programs that use statically-linked libraries are usually faster than those that use shared libraries. Programs that use shared libraries are usually slower than those that use statically-linked libraries.
In statically-linked programs, all code is contained in a single executable module. Therefore, they never run into compatibility issues. Dynamically linked programs are dependent on having a compatible library. If a library is changed (for example, a new compiler release may change a library), applications might have to be reworked to be made compatible with the new version of the library. If a library is removed from the system, programs using that library will no longer work.

Author: swamy2064

M. Pavan Kumar Swamy, Completed Advanced course in Embedded Systems From Vector Institute[Hyderabad], From ELURU.

Leave a comment