Skip to content

Setting Linux dynamic linker path

Ravi Mohan edited this page Feb 7, 2022 · 5 revisions

Definition: Dynamic linker is a Linux program which finds and loads the shared objects (shared libraries) needed by a program, prepare the program to run, and then run it. Linux binaries require dynamic linking (linking at run time) unless the -static option was given to ld(1) during compilation.

The dynamic linker runs implicitly when running a Linux binary. On the other hand, it can be run explicitly in the terminal

 /lib/ld-linux.so.*  [OPTIONS] [PROGRAM [ARGUMENTS]]

When resolving library dependencies, the dynamic linker first inspects each dependency string to see if it contains a slash (this can occur if a library pathname containing slashes was specified at link time). If a slash is found, then the dependency string is interpreted as a (relative or absolute) pathname, and the library is loaded using that pathname.

More information can be found here.

I shall focus on the Linux environment variable LD_LIBRARY_PATH. It is a a semicolon-separated set of directories where (shared in our case) libraries should be searched for first, before the standard set of directories; this is useful when debugging a new library or using a nonstandard library for special purposes. More generally LD_LIBRARY_PATH looks for the ELF libraries.

Case Study

Suppose we built an Linux executable with GCC version 11.2.0 and then we try to run it. Depending on the system setting (what are those?) we may get the following message

./Application: /usr/lib/x86_64-linux-gnu/libstdc++.so.6: version `GLIBCXX_3.4.29' not found (required by ./Application)

This means that the dynamic linker is unable to locate the relevant libraries and we need to modify the string of searchable paths separated by colon (the variable LD_LIBRARY_PATH) having libstdc++.so.6 with that version, in the string, like so

export LD_LIBRARY_PATH=/usr/local/lib64:$LD_LIBRARY_PATH

More information is here.

Clone this wiki locally