简体   繁体   English

如何强制在系统库上使用本地共享库?

[英]How to force using local shared libraries over system libraries?

How can I force using a local library over the system library in linux? 如何在linux中强制使用本地库而不是系统库?

I linked my executable explicitly to some .so files in my project/lib directory eg (../lib/libluajit.so). 我将我的可执行文件显式链接到我的project / lib目录中的一些.so文件,例如(../lib/libluajit.so)。

Running my executable under gdb or using ldd shows that it still uses the system libluajit-5.1.so.2 在gdb或使用ldd下运行我的可执行文件表明它仍然使用系统libluajit-5.1.so.2

I then set LD_LIBRARY_PATH to my project/lib directory and exported it, then ran my executable. 然后我将LD_LIBRARY_PATH设置为我的project / lib目录并导出它,然后运行我的可执行文件。 Somehow it's still picking up the system library (confirmed by both gdb and ldd) 不知怎的,它仍然在拿起系统库(由gdb和ldd确认)

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. 我想知道这是怎么可能的,以及我可以做些什么来强制它在我的project / lib目录中使用本地libluajit.so。

When you link, specify the directory of the library and also use an rpath: 链接时,指定库的目录并使用rpath:

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

-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告诉它在运行时搜索库的位置。

Note that -L and -rpath need the directory that contains your .so file, not the actual path of the library file itself. 请注意, -L-rpath需要包含.so文件的目录,而不是库文件本身的实际路径。

I have to question why you want to use your own version of a library over a system-provided version (having multiple different versions floating around is nothing but a recipe for trouble and user confusion). 我不得不质疑你为什么要在系统提供的版本上使用你自己的库版本(浮动多个不同的版本只是一个麻烦和用户混淆的方法)。

However, you should be able to export LD_PRELOAD=<path_to_your_shared_obj> to force it to load your own version. 但是,您应该能够export LD_PRELOAD=<path_to_your_shared_obj>以强制它加载您自己的版本。

Note that no mechanism to override library versions will persist through any sort of privilege elevation (for example sudo ). 请注意,任何覆盖库版本的机制都不会通过任何类型的权限提升(例如sudo )保留。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM