Static vs Dynamic Linking: Differences Explained

Linking joins your compiled code with the library code it uses. With static linking, the needed library code is copied into the executable at build time, so the program is self-contained. With dynamic linking, the executable only records which shared libraries it needs, and the operating system loads them when the program starts, so many programs can share one copy. Static is simpler to deploy; dynamic saves memory and makes updates easier.

AspectStatic linkingDynamic linking
When it happensAt build time: library code is copied into the executableAt load or run time: a dynamic linker finds and maps shared libraries (.so, .dll)
Executable sizeLargerSmaller
Memory useEach program carries its own copy of the library codeOne copy of a shared library can be shared by many processes
Updates and security fixesThe program must be rebuilt and redistributedUpdate the library once; programs benefit without rebuilding (if the interface stays compatible)
Start-up timeSlightly fasterSlightly slower: symbols must be resolved (often lazily)
DeploymentSelf-contained: runs without the libraries being installedNeeds the right library versions on the target system, or the program fails to start
Run-time costAllows whole-program optimization and direct callsSmall overhead from indirect calls through the GOT and PLT
Version problemsNone at run timePossible mismatches between the program and the installed library
Typical usesEmbedded firmware, single-file tools, minimal containersMost programs on general-purpose operating systems, plug-ins, system libraries such as libc

Figures are typical orders of magnitude and vary by process generation and product.

When to use which

Static
When you want a self-contained binary that runs anywhere, or when there is no operating system loader, as in embedded firmware.
Dynamic
When many programs share the same libraries and you want to save memory and patch libraries centrally.

FAQ

Why does my program fail with a missing library error?

It was dynamically linked, and the shared library it needs (a .so or .dll file) is not installed on that computer. Install the library or link the program statically.

Does dynamic linking save memory?

Yes. One copy of a shared library in RAM can be shared by many processes.

Is static linking faster?

Slightly. It avoids resolving symbols at start-up and the indirect calls through the GOT and PLT, and it allows whole-program optimization.

Why does dynamic linking help with security updates?

A shared library can be patched once, and every program that uses it benefits without being rebuilt.

Check your understanding

Which describes static linking?

  1. AThe code of the libraries used is copied into the executable
  2. BThe libraries are loaded and bound every time the program runs
  3. CAll processing is written in one source file without libraries
Show answer and explanation

Answer: A. The code of the libraries used is copied into the executable

With static linking, the linker extracts the needed functions from static libraries (.a files on Linux) and builds them into the executable. It runs even where the library is absent and needs no binding at start-up, but the executable grows and every program using the library carries its own copy. Fixes to the library are not picked up unless the program is rebuilt.

What is an advantage of dynamic linking (shared libraries)?

  1. AIt always runs even if the library is missing from the system
  2. BSeveral programs can share one library in memory, and updating the library applies fixes without rebuilding
  3. CNo start-up work is needed, so it always starts faster than static linking
Show answer and explanation

Answer: B. Several programs can share one library in memory, and updating the library applies fixes without rebuilding

With dynamic linking, the executable records only which functions of which shared libraries it uses, and at run time the dynamic linker loads the libraries and binds them. Shared library code is never modified, so one copy in physical memory can be shared by many processes. Fixing a bug or vulnerability just means replacing the library. Binding does, however, add work at start-up.

A dynamically linked program copied to another computer fails to start. What is the most likely cause?

  1. AThe executable contains all library code and is too large
  2. BDynamically linked programs are allowed to run only on the computer that built them
  3. CA required shared library is missing or only an incompatible version is installed
Show answer and explanation

Answer: C. A required shared library is missing or only an incompatible version is installed

A dynamically linked program uses the shared libraries on the computer where it runs. If a required library is absent, or only an older version lacking some functions is present, the dynamic linker cannot bind it and start-up fails. On Linux the ldd command shows which shared libraries a program needs and whether they are found. Some developers choose static linking deliberately to make distribution easier.

  1. When linking with a static library (.a file), what does the linker put into the executable?
  2. What is the role of the GOT (global offset table) used in shared libraries?
  3. When 100 processes use the same shared library, how is physical memory used?

Practice Linkers, Loaders & Executables questions

Key terms explained

Static linking
Linking that copies the code of the libraries used into the executable. It runs without the libraries present, but the file is larger and library fixes require rebuilding.
Dynamic linking
Linking that loads and binds shared libraries at run time. Libraries can be shared between processes and fixed by replacement, but a compatible library must be present on the system.
Shared library
A library loaded at run time by dynamic linking: .so on Linux, .dll on Windows. Built as position-independent code, its code pages can be shared by many processes.
Static library
An archive bundling many object files (.a on Linux). During static linking, the linker extracts only the objects it needs into the executable.

Related comparisons