在共享库搜索路径

在共享库搜索路径

本文介绍了在共享库搜索路径中查找目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望对特定目录中的每个共享库都使用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.

推荐答案

POSIX不支持在共享库搜索路径上查找目录的机制(例如,它不强制执行 LD_LIBRARY_PATH ),因此任何解决方案本质上都是特定于平台的.

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存在一些问题,因为要使用的值可能基于/etc/ld.so.conf 的内容以及 LD_LIBRARY_PATH 中的任何运行时值环境变量其他系统则存在类似的问题.默认位置也因系统而异-32位Linux机器通常使用/lib /usr/lib ,而/lib64 /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 等).

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 in LD_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 the sparcv9 directories). Solaris also has environment variables LD_LIBRARY_PATH_64 and LD_LIBRARY_PATH_32. HP-UX and AIX traditionally use other variables than LD_LIBRARY_PATH -- SHLIB_PATH and LIBPATH, IIRC -- though I believe AIX now uses LD_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).

因此,您的解决方案将针对特定平台.您将需要确定默认位置,要读取的环境变量,要读取的配置文件以及相关的文件扩展名.鉴于这些,则主要是SMOP-编程简单问题:

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())
      • 在相关文件的路径上使用 dlopen().
      • 进行任何与您相关的分析.
      • 使用 dlclose()

      另请参阅下面的注释中的注释...整个主题在各个平台上都有不同程度的变化.

      See also the notes in the comment below...the complete topic is modestly fraught with variations from platform to platform.

      这篇关于在共享库搜索路径中查找目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 02:10