如何在GCC Linux中指定非默认的共享库路径?加载共享库时出现“错误”运行时 [英] How to specify non-default shared-library path in GCC Linux? Getting "error while loading shared libraries" when running

查看:104
本文介绍了如何在GCC Linux中指定非默认的共享库路径?加载共享库时出现“错误”运行时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一台我没有root权限的笔记本电脑。



到机器上我使用 configure --prefix = $ HOME / .usr 来安装库。〜/ .usr / lib 中:


$ b

  libXX.so.16.0.0 
libXX.so.16
libXX.so
libXX.la
libXX .a

当我使用下面的命令编译一个程序来调用库提供的函数时:
gcc XXX.c -o xxx.out -L $ HOME / .usr / lib -lXX



xxx.out是在没有警告的情况下生成的,但是当我运行它时,会抛出这样的错误:



./ xxx.out:加载时出错共享库:libXX.so.16:无法打开共享目标文件:没有这样的文件或目录,尽管 libXX.so.16 驻留在那里。

我的线索简单的假设是,当xxx.out是时,〜/ .usr / lib 调用。
但我能做些什么来指定.so的路径,以便xxx.out可以在那里查找.so文件?



另外一个是当我将 -static 喂给gcc,另一个错误会发生,如下所示:

  undefined引用`function_proviced_by_the_very_librar'

看来 .so 即使给予gcc -L 和 -l <​​/ code>也无所谓。
我该怎么做才能用这个库建立一个可用的exe文件?






对于其他人来说,问题和我一样



我发现一篇关于tldp的有用文章



它引入了静态/共享/动态加载的库,以及一些使用它们的示例代码。 / b>

解决方案

有两种方法可以实现这一点:


  • 使用 -rpath 连结器选项:


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




  • 使用 LD_LIBRARY_PATH 环境变量 - 将此行放入〜/ .bashrc file:



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



即使对于预生成的二进制文件,这也可以工作,因此您可以为exa从 debian.org 下载一些软件包,将二进制文件和共享库解压到主目录中,然后启动它们而不用重新编译。



对于快速测试,您也可以(至少在bash中):

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

不改变你的库路径的好处。


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, I got these files in ~/.usr/lib :

libXX.so.16.0.0
libXX.so.16
libXX.so
libXX.la
libXX.a

when I compile a program that invokes one of function provided by the library with this command : gcc XXX.c -o xxx.out -L$HOME/.usr/lib -lXX

xxx.out was generated without warning, but when I run it error like this was thrown:

./xxx.out: error while loading shared libraries: libXX.so.16: cannot open shared object file: No such file or directory , though libXX.so.16 resides there.

my clue-less assumption is that ~/.usr/lib wasn't searched when xxx.out is invoked. but what can I do to specify path of .so , in order that xxx.out can look there for .so file?

An addition is when I feed -static to gcc, another error happens like this:

undefined reference to `function_proviced_by_the_very_librar'

It seems .so does not matter even though -L and -l are given to gcc. what should I do to build a usable exe with that library?


For other people who has the same question as I did

I found a useful article at tldp about this.

It introduces static/shared/dynamic loaded library, as well as some example code to use them.

解决方案

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.

这篇关于如何在GCC Linux中指定非默认的共享库路径?加载共享库时出现“错误”运行时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆