为什么每次运行应用程序时都必须使用导出来定义 LD_LIBRARY_PATH?

发布于 2024-07-16 05:15:36 字数 533 浏览 8 评论 0原文

我有一些使用一些共享库的代码(gcc 上的 c 代码)。 编译时,我必须使用 -I 和 -L 显式定义包含目录和库目录,因为它们不在标准位置。 当我尝试运行代码时,出现以下错误:

./sync_test 
./sync_test: error while loading shared libraries: libsync.so: cannot open shared object file: No such file or directory

但是,执行以下操作,一切正常:

export LD_LIBRARY_PATH="/path/to/library/"
./sync_test

现在,奇怪的部分是,这只有效一次。 如果我尝试再次运行sync_test,除非我先运行导出命令,否则我会收到相同的错误。 我尝试将以下内容添加到我的 .bashrc 中,但没有什么区别:

LD_LIBRARY_PATH="/path/to/library/"

I have some code that uses some shared libraries (c code on gcc). When compiling I have to explicitly define the include and library directories using -I and -L, since they aren't in the standard places. When I try to run the code, I get the following error:

./sync_test 
./sync_test: error while loading shared libraries: libsync.so: cannot open shared object file: No such file or directory

However, do the following, everything works just fine:

export LD_LIBRARY_PATH="/path/to/library/"
./sync_test

Now, the strange part is, this only works once. If I try and run sync_test again I get the same error unless I run the export command first. I tried adding the following to my .bashrc, but it made no difference:

LD_LIBRARY_PATH="/path/to/library/"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

等风来 2024-07-23 05:15:37

你在 .bashrc 中“导出”了吗?

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:"/path/to/library"

Did you 'export' in your .bashrc?

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:"/path/to/library"
千纸鹤带着心事 2024-07-23 05:15:37

您可以使用 rpath 将其烘焙到二进制文件本身中,而不是在运行时使用 LD_LIBRARY_PATH 覆盖库搜索路径。 如果您与 GCC 链接,添加 -Wl,-rpath, 应该可以解决问题,如果您与 ld 链接,则只需 -rpath

Instead of overriding the library search path at runtime with LD_LIBRARY_PATH, you could instead bake it into the binary itself with rpath. If you link with GCC adding -Wl,-rpath,<libdir> should do the trick, if you link with ld it's just -rpath <libdir>.

翻身的咸鱼 2024-07-23 05:15:37

如果您在系统上安装了该软件,您还可以做的是将包含共享库的目录添加到您的 /etc/ld.so.conf 文件中,或者在以下位置创建一个新文件: /etc/ld.so.conf.d/

(我都检查过 RHEL5 和 Ubuntu 发行版,所以我认为它对于 Linux 是通用的)

ldconfig 程序将确保它们在系统范围内包含。

请参阅以下链接了解更多信息:
www.dwheeler.com/secure-programs/Secure-Programs- HOWTO/dlls.html

What you also can do, if it's something you installed on your system, is to add the directory that contains the shared libraries to your /etc/ld.so.conf file, or make a new file in /etc/ld.so.conf.d/

(I've both checked RHEL5 and Ubuntu distribution so I think it's generic for linux)

The ldconfig program will make sure they are system-wide included.

See the following link for more information:
www.dwheeler.com/secure-programs/Secure-Programs-HOWTO/dlls.html

野稚 2024-07-23 05:15:37

您可以在代码中添加具有新定义的调用系统:

sprintf(newdef,"export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:%s:%s",ld1,ld2);
system(newdef);

但是,我不知道这是正确的解决方案,但它有效。

问候

You could add in your code a call system with the new definition:

sprintf(newdef,"export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:%s:%s",ld1,ld2);
system(newdef);

But, I don't know it that is the rigth solution but it works.

Regards

潦草背影 2024-07-23 05:15:36

您应该避免在 .bashrc 中设置 LD_LIBRARY_PATH。 有关详细信息,请参阅为什么 LD_LIBRARY_PATH 不好

链接时使用链接器选项 -rpath ,以便动态链接器知道在运行时在哪里找到libsync.so

gcc ... -Wl,-rpath /path/to/library -L/path/to/library -lsync -o sync_test

编辑:

另一种方法是使用这样的包装器

#!/bin/bash

LD_LIBRARY_PATH=/path/to/library sync_test "$@"

如果 sync_test 启动任何其他程序,它们最终可能会使用 /path/to/library 中的库,这可能或可能不是有意的。

You should avoid setting LD_LIBRARY_PATH in your .bashrc. See "Why LD_LIBRARY_PATH is bad" for more information.

Use the linker option -rpath while linking so that the dynamic linker knows where to find libsync.so during runtime.

gcc ... -Wl,-rpath /path/to/library -L/path/to/library -lsync -o sync_test

EDIT:

Another way would be to use a wrapper like this

#!/bin/bash

LD_LIBRARY_PATH=/path/to/library sync_test "$@"

If sync_test starts any other programs, they might end up using the libs in /path/to/library which may or may not be intended.

长发绾君心 2024-07-23 05:15:36

在你的 .bashrc 中使用

export LD_LIBRARY_PATH="/path/to/library/"

,否则,它只能用于 bash 而不能用于你启动的任何程序。

链接时尝试 -R/path/to/library/ 标志,它会让程序在该目录中查找,并且不需要设置任何环境变量。

编辑:看起来 -R 仅适用于 Solaris,而您使用的是 Linux。

另一种方法是将路径添加到 /etc/ld.so.conf 并运行 ldconfig。 请注意,这是一个全局更改,将应用于所有动态链接的二进制文件。

Use

export LD_LIBRARY_PATH="/path/to/library/"

in your .bashrc otherwise, it'll only be available to bash and not any programs you start.

Try -R/path/to/library/ flag when you're linking, it'll make the program look in that directory and you won't need to set any environment variables.

EDIT: Looks like -R is Solaris only, and you're on Linux.

An alternate way would be to add the path to /etc/ld.so.conf and run ldconfig. Note that this is a global change that will apply to all dynamically linked binaries.

清风无影 2024-07-23 05:15:36

你可以把这一切放在一行上:

LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/path/to/library" ./sync_test

应该让事情变得更容易,即使它不会改变任何根本性的东西

You can just put this all on one line:

LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/path/to/library" ./sync_test

Should make things a little easier, even if it doesn't change anything fundamental

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文