What are the differences between static and dynamic (shared) library linking?

Before understanding the difference between static and dynamic (shared) library linking let's see the life cycle of a typical program right from writing source code to its execution. A program is first written using any editor of programmer's choice in form of a text file, then it has to be compiled in order to translate the text file into object code that a machine can understand and execute.

The program we write might make use of other programs (which is usually the case), or libraries of programs. These other programs or libraries must be brought together with the program we write in order to execute it.

Linking is the process of bringing external programs together required by the one we write for its successful execution. Static and dynamic linking are two processes of collecting and combining multiple object files in order to create a single executable. Here we will discuss the difference between them. Read full article on static and dynamic linking for more details.

Linking can be performed at both compile time, when the source code is translated into machine code; and load time, when the program is loaded into memory by the loader, and even at run time, by application programs. And, it is performed by programs called linkers. Linkers are also called link editors. Linking is performed as the last step in compiling a program.

After linking, for execution the combined program must be moved into memory. In doing so, there must be addresses assigned to the data and instructions for execution purposes. The above process can be summarized as program life cycle (write -> compile -> link -> load -> execute).

Following are the major differences between static and dynamic linking:

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.

Hope you have enjoyed reading the difference between static and dynamic linking of libraries Please do write us if you have any suggestion/comment or come across any error on this page. Thanks for reading!



Share this page on WhatsApp

Get Free Tutorials by Email

About the Author

is the founder and main contributor for cs-fundamentals.com. He is a software professional (post graduated from BITS-Pilani) and loves writing technical articles on programming and data structures.