Difference between Static and Shared libraries

In programming, a library is a collection of pre-compiled code that can be reused by programs for specific functionality. Based on how the library code is linked and loaded, libraries are classified into two types − static libraries and shared (dynamic) libraries.

Static Library

A static library is a library whose code is copied directly into the target executable at compile time by the linker. The resulting executable is self-contained and does not depend on external library files at runtime. Static libraries use the extensions .a (Linux) or .lib (Windows).

Shared Library

A shared library (also called a dynamic library) is loaded into memory at runtime by the operating system. Only the address of the library is stored in the executable, and the actual library code resides in a shared memory location accessible by all programs. Shared libraries use the extensions .so (Linux) or .dll (Windows).

Static Library (.a / .lib) Executable App Code Library Code (copied in) Self-contained, larger size Linked at compile time Shared Library (.so / .dll) Executable App Code + ref Shared Library (in memory) Smaller executable, shared code Loaded at runtime by OS Depends on library being present

Key Differences

Feature Static Library Shared Library
Linking Time Compile time (copied into executable) Runtime (loaded by OS when program starts)
Executable Size Larger (library code embedded) Smaller (only reference stored)
Recompilation Required if library code changes Not required (just update the shared library)
Runtime Performance Slightly slower startup (loads entire binary) Faster if library is already in memory
Dependency Self-contained, no external dependency Depends on library file being present on system
File Extensions .a (Linux), .lib (Windows) .so (Linux), .dll (Windows)

Conclusion

Static libraries produce self-contained executables with no runtime dependencies but result in larger file sizes. Shared libraries reduce executable size and allow updates without recompilation, but the program depends on the library being available on the system at runtime.

Updated on: 2026-03-14T10:38:22+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements