Part II · Your system
23.1 Dynamic linking and ldd
alsolddshared libraryshared libraries.sold.soldconfigld_library_patherror while loading shared librariesdynamic linking
TL;DR
- Most programs don't contain the libraries they use: at start-up, the
dynamic loader finds the
.sofiles they need and loads them. ldd programlists those libraries and where each was found. "not found" marks the one that's missing.- The loader searches
LD_LIBRARY_PATHfirst, then the foldersldconfigknows (from/etc/ld.so.conf.d/), then the standard ones.
ldd /usr/bin/curl
ldconfig -p | grep libssl
On your machine
$ atfq 23.1 PATH /home/alice/.local/bin /usr/local/bin (doesn't exist) /usr/bin . ⚠ the current folder, wherever you are Repeated /usr/bin (listed more than once: harmless, the first one wins) Libraries LD_LIBRARY_PATH=/opt/oracle/lib Runs programs built for x86-64, glibc
An example: Alice's laptop, one of the test machines. In your terminal, this block shows your own machine, read live.
Pitfalls
- "error while loading shared libraries: libfoo.so.1": install the package that provides it, rather than copying or linking another version.
- After putting a library in a new folder, list that folder in a file in
/etc/ld.so.conf.d/, then runsudo ldconfig. - Don't run
lddon a program you don't trust: it may run the program's code.objdump -p program | grep NEEDEDonly reads it. LD_LIBRARY_PATHset for everything breaks other programs: set it only for the command that needs it.
Deep dive 2 questions
Why link dynamically?
One copy of a library serves every program, on disk and in RAM, and a security fix in the library fixes all of them at once. Static linking puts everything in the program: easier to copy to another machine, but bigger, and every program must be rebuilt to get a fix. Go programs are often linked that way.
What is the number in libfoo.so.1?
The major version, part of the library's "soname". A program asks for
libfoo.so.1; the file is often libfoo.so.1.2.3, and libfoo.so.1 is a
link to it. A new major version (.so.2) means an incompatible change, and
it can be installed next to the old one.