Search code examples
exportshared-librariessetenv

shared library - how to permanently set the path


I am using a program that has some shared library that are usually installed to /usr/lib.

However for some reason i have to have these libs locally. So to make my program run (which depends on the former) I need to export LD_LIBRARY_PATH= ... or add my local path permanently. This is ok for me but users of my software don't know this and for them this is too complicated. So my question: is there a way to automatically set the local path to my shared libs which are called by my program at runtime.


Solution

  • is there a way to automatically set the local path to my shared libs

    Maybe.

    If you are on Linux, and your application is installed together with shared library into say /some/prefix/bin/app and /some/prefix/lib/libsharedlib.so, then linking your application with:

    gcc -o app -Wl,--rpath='$ORIGIN/../lib' main.o ... -lsharedlib
    

    will achieve exactly the result you want (note: you can move both app and library into /another/dir, and it will still work so long as both the lib and bin directories are moved together).

    Note: single quotes around $ORIGIN are required.

    If you are on a platform that doesn't support $ORIGIN, the other common technique is to wrap the application in a shell script, which looks at $0, sets LD_LIBRARY_PATH appropriately, then execs the real application (which is often called app.bin, or app.exe).