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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 17:39:56  来源:igfitidea点击:

How can I find the full file path given a library name like libfoo.so.1?

clinuxlinker

提问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 lddand grepon a file that is knowingly using libGL.so.1, it looks like:

在故意使用 的文件上使用ldd和,它看起来像:greplibGL.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.1command 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 dlopenor 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 dladdron any symbol from the library will return information containing the full pathname that was loaded.

如果您不介意实际加载库并使用一些非标准但广泛使用的函数,调用dladdr库中的任何符号将返回包含加载的完整路径名的信息。

回答by HonkyTonk

Use ldconfigwhich is the tool that manages link space.

使用管理链接空间的工具ldconfig

The -pflag lets you browse all available linkable libraries.

-p标志可让您浏览所有可用的可链接库。

回答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 libcand 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: