运行时链接到不在LD

编程入门 行业动态 更新时间:2024-10-23 23:32:27
运行时链接到不在LD_LIBRARY_PATH上的动态库(Run-time linking to dynamic libraries not on LD_LIBRARY_PATH)

我正在尝试将我的项目链接到放置在项目的基本目录[proj_dir] / lib上的一组特定的自定义编译库 - 而不是在任何系统的/ lib,/ usr / lib或/ usr / local /上lib - 避免与相同库的已安装库存版本混淆。

我可以通过使用-L标志传递库路径来编译项目,但是error while loading shared libraries libXXX.so: cannot open shared object file: No such file or directory运行已编译的二进制error while loading shared libraries libXXX.so: cannot open shared object file: No such file or directory时error while loading shared libraries libXXX.so: cannot open shared object file: No such file or directory ,并且ldd告诉我它找不到那些特定的库。

另一方面,如果我将LD_LIBRARY_PATH=[proj_dir]/lib传递给可执行文件,我可以毫无问题地运行它。 还有,有没有办法隐式链接这些库,而不必在运行时手动设置LD_LIBRARY_PATH ?

I'm trying to link a project of mine to a particular set of custom-compiled libraries placed on the project's base directory [proj_dir]/lib - not on any of the system's /lib, /usr/lib or /usr/local/lib - to avoid conficts with the installed stock versions of those same libraries.

I'm able to compile the project by passing the library path with the -L flag, but I get error while loading shared libraries libXXX.so: cannot open shared object file: No such file or directory when I run the compiled binary, and ldd tells me it can't find those particular libs.

On the other hand, I am able to run it without issue if I pass LD_LIBRARY_PATH=[proj_dir]/lib to the executable. Still, is there a way to link those libraries implicitly, without having to manually set the LD_LIBRARY_PATH at runtime?

最满意答案

你也可以

在调用实际程序之前编写一个包装脚本以始终包含LD_LIBRARY_PATH (更灵活)。 将-Wl,-rpath=<directory>添加到链接器选项,以将目录添加到运行时库搜索路径。 所以假设你有一个libfoo.so并且你的程序和DSO应该位于同一个目录中,你的编译命令可能如下所示: gcc -o myprogam main.c -L. -lfoo -Wl,-rpath='$ORIGIN' gcc -o myprogam main.c -L. -lfoo -Wl,-rpath='$ORIGIN' 。

更新:正如Maxim正确指出的那样,设置-rpath=. 是危险的,应该避免 。

对于-Wl,请参阅gcc联机帮助页 ,有关-rpath请参阅ld联机帮助页 。

You can either

Write a wrapper script to always include LD_LIBRARY_PATH before calling the actual program (more flexible). Add -Wl,-rpath=<directory> to your linker options to add a directory to the runtime library search path. So assuming you have a libfoo.so and your program and DSO should lie in the same directory, your compilation command could look like this: gcc -o myprogam main.c -L. -lfoo -Wl,-rpath='$ORIGIN'.

Update: As correctly noted by Maxim, setting -rpath=. is dangerous and should be avoided.

For -Wl,, see the gcc manpage and for -rpath see the ld manpage.

更多推荐

本文发布于:2023-07-24 01:53:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1240137.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:链接   LD

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!