I have a shred library libcustom.so in a non standard folder, and a python package where I use ctypes.cdll.LoadLibrary("libcustom.so").
How can I set libcustom.so path at build time (something similar to rpath) ?
env LD_LIBRARY_PATH=/path/to/custom/lib python3 -c "import mypackage"
This works fine, but I don't want to use global LD_LIBRARY_PATH, and I don't want to set library path at run time.
python3 -c "import mypackage"
Results in an error:
OSError: libcustum.so: cannot open shared object file: No such file or directory
The only solution I have found (there must be better ones) is to create a python module to encapsulate the dlopen call. It is thus possible to insert an rpath at compilation time to resolve dynamic links at runtime.
We then add this extension to the python package:
Then, when building the package, we can insert the rpath:
It only remains to replace
ctypes.cdll.LoadLibrary("libcustom.so")byimportlib.import_module("mypackage._custom")._lib.