Linux 如何找到给定库名称(如 libfoo.so.1)的完整文件路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13148608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I find the full file path given a library name like libfoo.so.1?
提问by Lekensteyn
Without implementing a linker or using ldd
, how can I find the full path to a library? Is there a standard library available for that on Linux? (POSIX maybe?)
不实现链接器或使用ldd
,如何找到库的完整路径?Linux 上是否有可用的标准库?(可能是POSIX?)
Using ldd
and grep
on a file that is knowingly using libGL.so.1
, it looks like:
在故意使用 的文件上使用ldd
和,它看起来像:grep
libGL.so.1
$ ldd /usr/bin/glxinfo | grep libGL
libGL.so.1 => /usr/lib/libGL.so.1 (0x00007f34ff796000)
Given a library name like libGL.so.1
, how can I find the full path /usr/lib/libGL.so.1
?. Preferably accepting an option for finding 32-bit and 64-bit libraries. If no library does that, does a program exist to do this? Something like find-library-path libGL.so.1
. The locate libGL.so.1
command does not count.
给定一个库名称libGL.so.1
,我怎样才能找到完整路径/usr/lib/libGL.so.1
?. 最好接受用于查找 32 位和 64 位库的选项。如果没有图书馆这样做,是否有程序可以做到这一点?类似的东西find-library-path libGL.so.1
。该locate libGL.so.1
命令不计。
I don't want to actually load the library using dlopen
or something if it executes code from that library.
dlopen
如果它执行来自该库的代码,我不想实际加载使用或其他东西的库。
回答by R.. GitHub STOP HELPING ICE
If you don't mind actually loading the library and using some nonstandard but widely-available functions, calling dladdr
on any symbol from the library will return information containing the full pathname that was loaded.
如果您不介意实际加载库并使用一些非标准但广泛使用的函数,调用dladdr
库中的任何符号将返回包含加载的完整路径名的信息。
回答by HonkyTonk
回答by cLupus
Expanding on Honky Tonk's answer, the command echo "$(ldconfig -p | grep libGL.so.1 | tr ' ' '\n' | grep /)"
will give you the path alone.
扩展 Honky Tonk 的回答,该命令echo "$(ldconfig -p | grep libGL.so.1 | tr ' ' '\n' | grep /)"
将单独为您提供路径。
回答by saaj
For systems with GNU libc
and Python the following is the closest I found. It uses LD_DEBUG
(described in the man page of ld.so(8)
).
对于带有 GNUlibc
和 Python 的系统,以下是我发现的最接近的。它使用LD_DEBUG
(在的手册页中ld.so(8)
描述)。
LD_DEBUG=libs python3 -c "import ctypes; ctypes.CDLL('libssl.so.1.0.0')" 2>&1 | \
grep -A 1000 "initialize program: python" | grep -A 3 "find library"
The output (for libssl.so.1.0.0
) is the following:
输出(对于libssl.so.1.0.0
)如下:
15370: find library=libssl.so.1.0.0 [0]; searching
15370: search cache=/etc/ld.so.cache
15370: trying file=/lib/x86_64-linux-gnu/libssl.so.1.0.0
15370:
15370: find library=libcrypto.so.1.0.0 [0]; searching
15370: search cache=/etc/ld.so.cache
15370: trying file=/lib/x86_64-linux-gnu/libcrypto.so.1.0.0
15370: