How to specify non-default shared-library path in GCC Linux? Getting “error while loading shared libraries” when running

后端 未结 2 1542
半阙折子戏
半阙折子戏 2020-11-29 17:20

There is a laptop on which I have no root privilege.

onto the machine I have a library installed using configure --prefix=$HOME/.usr .

after that,

相关标签:
2条回答
  • 2020-11-29 17:45

    There are two ways to achieve that:

    • Use -rpath linker option:

    gcc XXX.c -o xxx.out -L$HOME/.usr/lib -lXX -Wl,-rpath=/home/user/.usr/lib

    • Use LD_LIBRARY_PATH environment variable - put this line in your ~/.bashrc file:

      export LD_LIBRARY_PATH=/home/user/.usr/lib

    This will work even for a pre-generated binaries, so you can for example download some packages from the debian.org, unpack the binaries and shared libraries into your home directory, and launch them without recompiling.

    For a quick test, you can also do (in bash at least):

    LD_LIBRARY_PATH=/home/user/.usr/lib ./xxx.out
    

    which has the advantage of not changing your library path for everything else.

    0 讨论(0)
  • 2020-11-29 17:55

    Should it be LIBRARY_PATH instead of LD_LIBRARY_PATH. gcc checks for LIBRARY_PATH which can be seen with -v option

    0 讨论(0)
提交回复
热议问题