What happens when compiling against a shared library?

gcc, ld

Solution

The shared libraries need to be fed to the linker's command line so that a reference to the specific functions and the file in which these functions reside, is stored into the executable. When the executable is run, the dynamic linker (`/lib/ld-linux.so`, `/libexec/ld-elf.so`, etc, depending on your system) is loaded first and checks these references. Once it finds the lib files, it maps them (using the `mmap()` system call) to your program's adress space.

You can see these references by running

objdump -T a.out

or

nm -D a.out

For ELF executables, the existence of the `.interp` section implies that the program uses dynamic linking.

Problem

I understand that when linking against a static library i.e. libname.a, the binary code for the used functions is taken out of the archive and inserted in the application binary. Therefore, the static library MUST be present at compilation time. However, with shared libraries I am lost. The function definitions are not copied. Then why is it needed that the shared library be provided on the linker command line? Also, are there different ways to link against shared libraries and what are they?

Original source