Difference Between Static And Dynamic Linking

What Is Static 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 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. Therefore, when the program is loaded, the operating system places a single file into the memory, which contains the executable code and data. This statically linked file includes both the calling program and the called program.

Facts About Static Linking

  • Static linking is the process of copying all library modules used in the program into the final executable image.
  • In static linking if any of the external programs has changed then they have to be recompiled and re-linked again or else the changes won’t reflect in existing executable file.
  • Static linking is performed by programs called linkers as the last step in compiling a program. Linkers are also called link editors.
  • Statically linked program takes constant load time every time it is loaded into the memory for execution.
  • In statically-linked programs, all code is contained in a single executable module. Therefore, they never run into compatibility issues.
  • Programs that use statically-linked libraries are usually faster than those that use shared libraries.
  • Statically linked files are significantly larger in size because external programs are built into the executable files.
  • Statistically linked files are larger in size.

What Is Dynamic Linking ?

Dynamic linking consists of compiling and linking code into a form that is loadable by programs at run time as well as link time. 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.

Dynamic linking uses a special variation of a library format referred to as dynamic linking library (DLL), which is a common way to partition applications and subsystems into smaller portions, which can be compiled, tested, re-used, managed, deployed and installed separately. This approach is very popular with Microsoft operating systems and came about to limit the size of application executable files and also their run-time images.

Facts About Dynamic Linking

  • In dynamic linking the names of the external libraries or shared libraries are placed into the memory. Dynamic linking lets many programs use single copy of executable module.  
  • In dynamic linking shared modules can be updated and recompiled. This is one of the greatest advantages dynamic linking offers.
  • Dynamic linking is performed at run time by the operating system.
  • In dynamic linking load time might be reduced if the shared library code is already present in memory.
  • 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.
  • Programs that use shared libraries are usually slower than those that use statically-linked libraries.
  • 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.
  • Dynamically linked files are smaller in size.

Also Read: Difference Between Linker And Loader

Difference Between Static And Dynamic Linking In Tabular Form

BASIS OF COMPARISON STATIC LINKING DYNAMIC LINKING
Description Static linking is the process of copying all library modules used in the program into the final executable image.   In dynamic linking the names of the external libraries or shared libraries are placed into the memory. Dynamic linking lets many programs use single copy of executable module.    
Shared Modules In static linking if any of the external programs has changed then they have to be recompiled and re-linked again or else the changes won’t reflect in existing executable file.   In dynamic linking shared modules can be updated and recompiled.
Execution 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.  
Load Time 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.  
Compatibility Factor 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.
Speed 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.  
Size of Executable Program 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.  
Size Of Files Statistically linked files are larger in size.   Dynamically linked files are smaller in size.