Advertisement
Advanced C++, Compilation & Linking

What is the difference between static and dynamic (shared) libraries?

Short answer

A static library (.a/.lib) is copied into the executable at link time—larger binary, no runtime dependency, updates require relinking.

Save C++ mock interview
Advertisement
AModel answer
A static library (.a/.lib) is copied into the executable at link time—larger binary, no runtime dependency, updates require relinking. A dynamic/shared library (.so/.dll) is linked at load/run time—smaller executables, shared across programs, updatable without relinking, but the library must be present at runtime and version compatibility matters. The choice trades deployment simplicity against flexibility and size.
Example

g++ main.o -lmylib # link against libmylib (static or shared)

Real-world example
Shipping a plugin as a shared library lets it be updated without rebuilding the host application.
Likely follow-ups
  • When is static linking preferable? | What runtime requirement do shared libraries add?
Related topics