如何通过系统库强制使用本地共享库? [英] How to force using local shared libraries over system libraries?

查看:78
本文介绍了如何通过系统库强制使用本地共享库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Linux中的系统库上强制使用本地库?

How can I force using a local library over the system library in linux?

我将可执行文件显式链接到我的project/lib目录中的某些.so文件,例如(../lib/libluajit.so).

I linked my executable explicitly to some .so files in my project/lib directory e.g. (../lib/libluajit.so).

在gdb下或使用ldd运行我的可执行文件表明它仍在使用系统libluajit-5.1.so.2

Running my executable under gdb or using ldd shows that it still uses the system libluajit-5.1.so.2

然后我将LD_LIBRARY_PATH设置到我的project/lib目录并导出它,然后运行我的可执行文件.仍然以某种方式仍在使用系统库(由gdb和ldd确认)

I then set LD_LIBRARY_PATH to my project/lib directory and exported it, then ran my executable. Somehow it's still picking up the system library (confirmed by both gdb and ldd)

我想知道这是怎么可能的,以及我可以做些什么来迫使它使用我的project/lib目录中的本地libluajit.so.

I'd like to know how that's even possible, and what I can do to force it to use the local libluajit.so in my project/lib directory.

推荐答案

链接时,请指定库的目录并使用rpath:

When you link, specify the directory of the library and also use an rpath:

-Wl,-rpath,/absolute/path/to/your/library -L/absolute/path/to/your/library -llibrary

-L告诉链接器在链接时在何处查找库,而-rpath告诉它在运行时在何处搜索库.

-L tells the linker where to find your library at link time, and -rpath tells it where to search for the library at runtime.

请注意,-L-rpath需要包含.so文件的目录,而不是库文件本身的实际路径.

Note that -L and -rpath need the directory that contains your .so file, not the actual path of the library file itself.

这篇关于如何通过系统库强制使用本地共享库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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