简体   繁体   English

确定用dlopen打开的动态库路径的可移植方法

[英]Portable way to determine path to dynamic library opened with dlopen

I'm searching for a portable (POSIX) way to determine the path to a library opened with dlopen() . 我正在寻找一种可移植(POSIX)的方法来确定使用dlopen()打开的库的路径。 Functions like dl_iterate_phdr() and dladdr() are not portable. dl_iterate_phdr()dladdr()类的函数不可移植。

Some man pages detail the path taken by dlopen() to find libraries to open ( ld.so.cache , LD_LIBRARY_PATH , /lib, /usr/lib, ...), so checking all of these directories could be possible, but not portable. 有些man页详细的路径dlopen()找到库打开( ld.so.cacheLD_LIBRARY_PATH ,/ lib目录,/ usr / lib目录,...),所以检查所有这些目录可能是可能的,但不便携式的。 For example, under Linux, I'd need a way to parse ld.so.cache but under Mac OS X, there is no such concept. 例如,在Linux下,我需要一种解析ld.so.cache的方法,但是在Mac OS X下,没有这样的概念。

Please see a previous question for detail on why I want to do this, since using dlopen is ill-advised in the general case. 有关我为什么要执行此操作的详细信息,请参见上一个问题 ,因为在一般情况下,建议不要使用dlopen

There's not really any way to do what you want portably. 根本没有任何方法可以随身携带您想要的东西。 Really it's probably a bad idea to use dlopen without an absolute path, and also a bad idea to use it for anything other than modules intended to be loaded with dlopen (created by and distributed with either your own project itself or another library your application uses). 确实,在没有绝对路径的情况下使用dlopen可能是一个坏主意,并且将其用于除打算用dlopen加载的模块以外的其他任何东西(由您自己的项目本身或应用程序使用的另一个库创建并分发)也是一个坏主意。 )。 If you use it with the system library search path and libraries "already present" on the system, you run serious risks of loading the wrong version or even the wrong library with the same name. 如果将它与系统库搜索路径一起使用,并且系统上的库“已经存在”,则冒着装入错误版本甚至同名库的风险。 As long as you always pass an absolute path to dlopen , you can be sure you know exactly what file was loaded (or better yet, know exactly what file will be loaded before it gets loaded). 只要始终向dlopen传递绝对路径,就可以确保确切知道已加载了什么文件(或者更好的是,确切知道要在加载文件之前加载什么文件)。

If this answer doesn't help, perhaps you could better explain what you're trying to achieve.. 如果这个答案没有帮助,也许您可​​以更好地解释您要达到的目标。

The simple answer is that there isn't one. 简单的答案是,没有答案。 This is one place where Windows is in advance of Unix. 这是Windows优于Unix的地方。

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

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