如果我在同一个应用程序运行中对同一个库/文件使用 dlopen 两次,在这两种情况下它会产生相同的句柄吗?对此有任何保证吗(一个简短的实验表明它至少在我的盒子上如此)?
我目前正在玩一个小插件系统(出于好奇),如果对这种观察到的行为有某种保证,我可以使用这个地址作为插件的密钥来防止重复加载。
是的。这dlopen(3)Linux 手册页说:
If the same library is loaded again with dlopen(), the same file
handle is returned. The dl library maintains reference counts for
library handles, so a dynamic library is not deallocated until
dlclose() has been called on it as many times as dlopen()
has succeeded on it.
顺便说一句,在 Linux 系统上,您可以 dlopen 很多(数十万)共享库,如我的示例manydl.c演示。主要限制是地址空间。
所以实际上,不用担心dlclose
-ing 的东西是可能的。
(unless your dlopen-ed shared libraries have weird or resource consuming constructor or destructor functions)
Added in December 2017:
请注意,相关的是传递给的确切路径字符串dlopen
。所以如果你使用"./foo.so"
and "././foo.so"
(or "../foosymlink.so"
where foosymlink.so
是一个符号链接foo.so
)dlopen 处理的句柄不同,在某些情况下,该共享库的两个实例可能会发生奇怪的行为。
added in june 2019:
另请阅读 Drepper 的如何编写共享库论文(它也很好地解释了如何use them!).
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)