在共享库搜索路径中查找目录
我想要 dlopen() 特定目录中的每个共享库。为了做到这一点,
检索 linux 的库搜索路径的最干净的方法是什么。或者是否有更快的方法来查找该路径中的特定目录?
posix 会更好。
I want dlopen() every shared library in a specific directory. In order to do that,
what is the cleanest way to retrieve linux's library search path. Or Is there a quicker way of find a specific directory in that path ?
posix would be better.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
POSIX 不支持查找共享库搜索路径上的目录的机制(例如,它不强制
LD_LIBRARY_PATH
),因此任何解决方案本质上都是特定于平台的。Linux 存在一些问题,因为要使用的值可能基于 /etc/ld.so.conf 的内容以及 LD_LIBRARY_PATH 环境变量中的任何运行时值;其他系统也存在类似的问题。默认位置也因系统而异 -
/lib
和/usr/lib
通常用于 32 位 Linux 机器,但/lib64
和 < code>/usr/lib64 至少在某些 64 位机器上使用。但是,其他平台使用 64 位软件的其他位置。例如,Solaris 使用/lib/sparcv9
和/usr/lib/sparcv9
(尽管文档提到了/lib/64
和/usr/lib/64
,它们是sparcv9
目录的符号链接)。 Solaris 还具有环境变量LD_LIBRARY_PATH_64
和LD_LIBRARY_PATH_32
。 HP-UX 和 AIX 传统上使用LD_LIBRARY_PATH
之外的其他变量 -SHLIB_PATH
和LIBPATH
、IIRC - 尽管我相信 AIX 现在使用LD_LIBRARY_PATH 也是如此。并且,在 Solaris 上,用于配置共享库的工具是“crle”(配置运行时链接环境),
/etc/ld.so.conf
的类似工具是/var/ld/ ld.config
或/var/ld/64/ld.config
。当然,共享库的扩展名也各不相同(.so
、.sl
、.dylib
、.bundle
> 等)。因此,您的解决方案将是特定于平台的。您需要决定默认位置、要读取的环境变量、要读取的配置文件以及相关的文件扩展名。鉴于这些,那么它主要是一个 SMOP - 简单的编程问题:
opendir()
)readdir()
)dlopen()
。dlclose()
closedir()
另请参阅下面评论中的注释...完整的主题在不同平台之间存在一定的差异。
POSIX does not support a mechanism to find out the directories on the shared library search path (it does not mandate
LD_LIBRARY_PATH
, for example), so any solution is inherently somewhat platform specific.Linux presents some problems because the values to be used could be based on the contents of
/etc/ld.so.conf
as well as any runtime value inLD_LIBRARY_PATH
environment variable; other systems present comparable problems. The default locations also vary by system - with/lib
and/usr/lib
being usual for 32-bit Linux machines but/lib64
and/usr/lib64
being used on at least some 64-bit machines. However, other platforms use other locations for 64-bit software. For example, Solaris uses/lib/sparcv9
and/usr/lib/sparcv9
, for example (though the docs mention/lib/64
and/usr/lib/64
, they're symlinks to thesparcv9
directories). Solaris also has environment variablesLD_LIBRARY_PATH_64
andLD_LIBRARY_PATH_32
. HP-UX and AIX traditionally use other variables thanLD_LIBRARY_PATH
--SHLIB_PATH
andLIBPATH
, IIRC -- though I believe AIX now usesLD_LIBRARY_PATH
too. And, on Solaris, the tool for configuring shared libraries is 'crle' (configure runtime linking environment) and the analog of/etc/ld.so.conf
is either/var/ld/ld.config
or/var/ld/64/ld.config
. Also, of course, the extensions on shared libraries varies (.so
,.sl
,.dylib
,.bundle
, etc).So, your solution will be platform-specific. You will need to decide on the the default locations, the environment variables to read, and the configuration file to read, and the relevant file extension. Given those, then it is mainly a SMOP - Simple Matter Of Programming:
opendir()
)readdir()
) in turndlopen()
on the path of the relevant files.dlclose()
closedir()
See also the notes in the comment below...the complete topic is modestly fraught with variations from platform to platform.
我不确定是否可以做到这一点并且可以便携。由于这个问题是关于 Linux 的,因此可移植性可能不是最重要的。那我就不明白POSIX约束了。你能澄清一下吗?
您可能必须实现
man 8 ld.so
中详细介绍的搜索功能,其中除了LD_LIBRARY_PATH
之外还包括扫描 /etc/ld.so.conf,或者让/lib/ld.so
做你想做的事并解析输出。一个不太漂亮的命令行可能是:然后您可以使用 POSIX 调用
opendir(3)
和readdir(3)
枚举文件。I'm not sure it's possible to do that and be portable. Since this question is about Linux, portability may not be of paramount importance. Then I do not understand the POSIX constraint. Could you clarify?
You'll probably have to either implement the search functionality detailed in
man 8 ld.so
, which includes scanning /etc/ld.so.conf in addition toLD_LIBRARY_PATH
, or make/lib/ld.so
do what you want for you and parse the output. A not-exactly-pretty command line for that could be:You can then enumerate files with the POSIX calls
opendir(3)
andreaddir(3)
.