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

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

问题描述

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

在我使用 configure --prefix=$HOME/.usr 安装了一个库的机器上.

之后,我在 ~/.usr/lib 中得到了这些文件:

libXX.so.16.0.0libXX.so.16libXX.solibXX.la库XX.a

当我编译一个程序时,该程序会使用此命令调用库提供的功能之一:gcc XXX.c -o xxx.out -L$HOME/.usr/lib -lXX

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

./xxx.out: error while loading shared libraries: libXX.so.16: cannot open shared object file: No such file or directory ,虽然 libXX.so.16 驻留在那里.

我毫无头绪的假设是在调用 xxx.out 时没有搜索 ~/.usr/lib.但是我该怎么做才能指定 .so 的路径,以便 xxx.out 可以在那里查找 .so 文件?

此外,当我将 -static 提供给 gcc 时,会发生另一个错误,如下所示:

对`function_proviced_by_the_very_librar'的未定义引用

即使 -L-l 被赋予 gcc,似乎 .so 也无关紧要.我应该怎么做才能用那个库构建一个可用的 exe?


对于和我有同样问题的其他人

我在 tldp 上找到了一篇关于此的有用文章.>

它介绍了静态/共享/动态加载的库,以及一些使用它们的示例代码.

解决方案

有两种方法可以实现:

  • 使用 -rpath 链接器选项:

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

  • 使用 LD_LIBRARY_PATH 环境变量 - 将此行放在您的 ~/.bashrc 文件中:

    导出 LD_LIBRARY_PATH=/home/user/.usr/lib

这甚至适用于预先生成的二进制文件,因此您可以例如从 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天全站免登陆