Dynamic Libraries and the Linker
Learn how to create and use dynamic libraries in C, understand the differences between static and dynamic linking, and explore loading shared libraries at runtime to write modular and efficient programs.
So far, we’ve been combining multiple files into one executable at compile time. But C also allows part of a program to be linked later, even while it’s running. In this lesson, we’ll explore how this is done using dynamic libraries, a feature that makes programs smaller, easier to update, and more modular.
Static linking vs. dynamic linking
In the previous lessons, when we compiled:
gcc -o go go.c primes.c
The compiler and linker combined everything into a single executable. This is called static linking, which means that all required code becomes part of the final program at compile time.
Dynamic linking works differently. Instead of copying library code into the executable, the program refers to external shared libraries that are loaded either when the program starts or while it is running. Here, shared libraries referred to the .so files on Linux (shared object) or .dll files on ...