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

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

问题描述

如何在 linux 中强制使用本地库而不是系统库?

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

我将我的可执行文件显式链接到我的项目/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 设置为我的项目/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天全站免登陆