difference between dynamic loading and dynamic linking?

This answer assumes that you know basic Linux command.

In Linux, there are two types of libraries: static or shared.

enter image description here

In order to call functions in a static library you need to statically link the library into your executable, resulting in a static binary.

While to call functions in a shared library, you have two options.

First option is dynamic linking, which is commonly used – when compiling your executable you must specify the shared library your program uses, otherwise it won’t even compile. When your program starts it’s the system’s job to open these libraries, which can be listed using the ldd command.

The other option is dynamic loading – when your program runs, it’s the program’s job to open that library. Such programs are usually linked with libdl, which provides the ability to open a shared library.

Excerpt from Wikipedia:

Dynamic loading is a mechanism by which a computer program can, at run
time, load a library (or other binary) into memory, retrieve the
addresses of functions and variables contained in the library, execute
those functions or access those variables, and unload the library from
memory. It is one of the 3 mechanisms by which a computer program can
use some other software; the other two are static linking and dynamic
linking. Unlike static linking and dynamic linking, dynamic loading
allows a computer program to start up in the absence of these
libraries, to discover available libraries, and to potentially gain
additional functionality.

If you are still in confusion, first read this awesome article: Anatomy of Linux dynamic libraries and build the dynamic loading example to get a feel of it, then come back to this answer.

Here is my output of ldd ./dl:

linux-vdso.so.1 =>  (0x00007fffe6b94000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f400f1e0000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f400ee10000)
/lib64/ld-linux-x86-64.so.2 (0x00007f400f400000)

As you can see, dl is a dynamic executable that depends on libdl, which is dynamically linked by ld.so, the Linux dynamic linker when you run dl. Same is true for the other 3 libraries in the list.

libm doesn’t show in this list, because it is used as a dynamically loaded library. It isn’t loaded until ld is asked to load it.

Leave a Comment