HowTo: Hard code the path to a dynamic library in the executable

De LMDZPedia
Aller à : navigation, rechercher

If you don't want to use the LD_LIBRARY_PATH environment variable (this is however the recommended way to proceed) to let your executable know where to find needed dynamic libraries (*.so shared object libraries), you can hard code the paths at compile (linking) time, with the -Wl,rpath= option.

general illustrative example

You want your executable to always first look for the needed library in /my/personal/stuff/lib and /some/other/place/lib , then compile with:

-Wl,rpath=/my/personal/stuff/lib:/some/other/place/lib

The drawback of this method is that if the libraries are later moved to different locations, your executable won't know about it and probably abort with an error message of the likes of:

error while loading shared libraries: ...

LMDZ-specific example

Since compilation options are set in the arch.fcm file, this is where the option -Wl,rpath=... should be set, on the line starting with %BASE_LD

For example if one wants to hard code to look for personal netcdf libraries that are located in /my/personal/netcdf/lib:

%BASE_LD -Wl,-rpath=/my/personal/netcdf/lib

Not that it is generally not recommended to use this option as if later on the target library is moved to a different location then the executable won't work anymore... You have been warned.

05/07/2022