Shared library

Also known as: dynamic library

On this page 7

A shared library is a file of compiled, reusable code that the operating system loads into a running program at load time or run time, so that many programs can use a single copy instead of embedding their own. Among the software fundamentals, it is the main way operating systems let applications share common functionality, and it is also called a dynamic library because the final binding happens dynamically rather than when the program is built.

Familiar examples are DLL files on Windows, .so files on Linux and .dylib files on macOS. Each contains machine code, a table of exported symbols and metadata that tells the loader what the library itself depends on. The concept sits between the broader idea of a software library and the executable file that ultimately uses it.

How a shared library is loaded and linked

A shared library works by deferring symbol resolution from build time to the moment the program starts or calls the code. When a developer links against a shared library, the linker records only a reference to the library name and the functions used, not the code itself. The resulting executable file is therefore smaller and incomplete on its own.

At startup, the dynamic loader (for example ld.so on Linux, dyld on macOS or the Windows loader) reads the list of required libraries, searches for them in configured directories and maps them into the process address space. It then fills in addresses through tables such as the Global Offset Table and Procedure Linkage Table on ELF systems or the Import Address Table on Windows. Many loaders use lazy binding, resolving a function only on its first call to shorten startup.

Because the code is mapped from the same file, the operating system can keep one physical copy of read-only pages in memory and share it across processes. Writable data, such as global variables, is copied per process through copy-on-write, so programs do not interfere with each other’s state.

The table below summarizes the main formats and the loader that handles each.

PlatformFile extensionBinary formatDynamic loader
Windows.dllPE/COFFWindows loader in ntdll
Linux.soELFld-linux.so
macOS.dylib or frameworkMach-Odyld
Android.soELFBionic linker
FreeBSD.soELFld-elf.so
Shared library formats by operating system

Shared library compared with static library

The essential difference is when the code becomes part of the program: a static library is copied into the executable at build time, while a shared library stays a separate file that is resolved later. This single choice affects size, update policy, startup speed and distribution.

The comparison below shows the practical consequences of each linking model.

PropertyStatic libraryShared library
Typical file.a or .lib.so, .dll or .dylib
Code locationCopied into each executableSeparate file loaded at run time
Executable sizeLargerSmaller
Memory use across programsOne copy per programOne shared copy of code pages
Security updatesRequire rebuilding every programReplace one library file
Startup costMinimalSymbol lookup and relocation
Deployment riskSelf-containedLibrary must be present and compatible
Static and shared linking compared

Neither model is universally better. Static linking favors portability and predictable behavior, which is why many container images and command-line tools written in Go or Rust ship as single binaries. Shared linking favors maintainability across a whole system, which is why Linux distributions build almost every package against shared system libraries.

Versioning and the application binary interface

A shared library remains safe to replace only while it keeps a stable application binary interface (ABI), meaning the same function signatures, data structure layouts and calling conventions that existing programs were compiled against. Changing a struct field order or removing an exported function breaks the ABI even when the source code still compiles.

Unix-like systems handle this through the soname convention: a file such as libexample.so.3.2.1 declares the soname libexample.so.3, and the major number changes only on incompatible releases. Programs record the soname, so several major versions can coexist on one system. GNU systems add symbol versioning, letting one library export old and new variants of the same function.

The following table lists the version components and what a change in each signals.

Version partExample changeCompatibility signal
Major3 to 4ABI break; programs must be relinked
Minor3.2 to 3.3New functions added; old ones intact
Patch3.2.1 to 3.2.2Bug or security fix; same interface
Meaning of shared library version numbers

Windows historically lacked an equivalent mechanism, which produced the problem known as DLL hell, where installing one application overwrote a DLL that another needed. Side-by-side assemblies, application-local DLLs and app packaging later reduced the issue.

Loading a shared library explicitly at run time

Besides automatic loading at startup, a program can open a shared library on demand through an API such as dlopen and dlsym on POSIX systems or LoadLibrary and GetProcAddress on Windows. This explicit dynamic loading is the basis of most plug-in architectures, optional features and hardware-specific code paths.

  • Plug-in systems load extensions found in a directory without rebuilding the host.
  • Optional dependencies are loaded only when a feature is used, so missing files do not prevent startup.
  • CPU-specific variants select an optimized build, for example one using AVX2 instructions, at run time.
  • Language runtimes such as Python load native extension modules as shared libraries.

Risks, failure modes and common mistakes

Most shared library problems come from a mismatch between what the program expects and what the loader finds. Because the dependency is resolved outside the build, errors appear only on the target machine, often as cryptic loader messages.

The table below pairs typical symptoms with their cause and remedy.

SymptomLikely causeFix
Library not found at startupFile missing or outside the search pathInstall the package or set rpath correctly
Undefined symbol errorOlder library version lacks a functionInstall a compatible version
Crash after an updateSilent ABI break in a minor releasePin the version and report the break
Wrong library loadedSearch order picks an unexpected copyUse absolute paths or safe search settings
Slow startupHundreds of libraries and relocationsPrelink, reduce dependencies or link statically
Common shared library errors and fixes

Search order is also a security concern. DLL search-order hijacking and misuse of LD_PRELOAD let an attacker place a malicious library where the loader looks first. Hardened systems restrict search paths, require signed libraries and ignore preload variables for privileged programs.

In plain words

A shared library is like a reference book in a public library instead of a copy on every desk. Many programs need the same routines, such as drawing windows, reading files or encrypting data. Rather than each program carrying its own copy, they all borrow the same file when they run.

This saves disk space and memory, and when the book is corrected, everyone reading it gets the fix at once. The catch is that every program depends on that shared copy being present and compatible. If someone swaps it for a very different edition, programs that expected the old one can stop working.

A real-world example

A system administrator manages about 200 Linux servers running web services, monitoring agents and scripting tools. A serious vulnerability is announced in the OpenSSL cryptography library, which every one of those programs uses through the shared file libssl.so.3.

Because the services are dynamically linked, the administrator installs the patched OpenSSL package through the distribution package manager and restarts the affected services. One library update fixes dozens of programs on each server within an hour, without recompiling anything.

A small internal tool that had been built with OpenSSL statically linked is the exception. It keeps the vulnerable code until a developer rebuilds and redeploys it, which shows in practice why shared linking simplifies security maintenance across a large fleet.