WhatIs: The LD LIBRARY PATH environment variable

De LMDZPedia
Révision de 27 septembre 2021 à 10:17 par Emillour (discussion | contributions)

(diff) ← Version précédente | Voir la version actuelle (diff) | Version suivante → (diff)
Aller à : navigation, rechercher

When a program runs, it looks for the dynamically linked libraries (*.so files) it needs.

The information on where to look for is stored in the LD_LIBRARY_PATH environment variable, which contains the list of paths to scan, e.g.:

LD_LIBRARY_PATH=/some/path/to/lib:/some/other/path/to/lib

If the program fails to find a sought library, then it will not start and stop with an error message of the likes of:

error while loading shared libraries: libnetcdff.so

In the example above libnetcdf.so was not found in any of the directories listed in LD_LIBRARY_PATH. Simply updating LD_LIBRARY_PATH with the correct path (for the sake of this example let's say libnetcdff.so is located in /my/personal/libraries/netcdf/lib):

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/my/personal/libraries/netcdf/lib

will solve the problem. Note that you have to first update the LD_LIBRARY_PATH environment variable and the run your program.

To know which *.so libraries an executable (e.g. mycode.e) needs and where it finds them:

ldd mycode.e

Note that an alternative to having to specify the paths in LD_LIBRARY_PATH is to specify the path at compile time (at the linking step) using the -Wl,-rpath= option

27/09/2021