Sobes.tech
Junior — Middle

What are the differences between static and dynamic libraries in the context of their usage and loading into a program?

sobes.tech AI

Answer from AI

Static libraries (.lib, .a) are linked to the program at compile time. The code from the static library is copied into the executable file, which increases the final file size but allows running the program without additional dependencies. Loading occurs immediately at program startup.

Dynamic libraries (.dll, .so) are loaded during program execution. The program contains references to these libraries, and the code is loaded into memory as needed. This reduces the executable size and allows updating the library without recompiling the program, but requires the library to be present in the system at runtime.

Example:

  • Static: gcc main.c libfoo.a -o app
  • Dynamic: gcc main.c -lfoo -o app and libfoo.so must be available at runtime.