使用dlopen()动态加载共享对象 [英] Dynamic loading of shared objects using dlopen()

查看:176
本文介绍了使用dlopen()动态加载共享对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个普通的X11应用程序.

I'm working on a plain X11 app.

默认情况下,我的应用仅需要libX11.so以及标准的gcc C和数学库. 该应用程序可以扩展Xfix,Xrender和ALSA声音系统的功能. 但是,这些功能(Xfix,Xrender和ALSA)是可选的.

By default, my app only requires libX11.so and the standard gcc C and math libs. The App can extend features with Xfixes, Xrender and ALSA sound system. However, these (Xfixes, Xrender and ALSA) feature are optional.

要实现此行为,我正在使用运行时加载,即libXfixes,libXrender和libasound必须进行dlopen()处理.

To achieve this behavior, I am using run time loading i.e., libXfixes, libXrender and libasound shall be dlopen()ed.

因此,在没有这样的库的情况下,该应用程序可以运行.

Hence the App can function in absence of such libraries.

现在我的问题:

What library names should I use when calling dlopen()?  

我发现这些版本在发行版之间有所不同.
例如,在openSUSE 11上,它们的名称如下:

I've observed that these differ from distro to distro.
For example, on openSUSE 11, they're named the following:

  • libXfixes.so
  • libXrender.so
  • libasound.so

但是,在Ubuntu上,名称中附加了一个版本号,如下所示:

On Ubuntu, however, the names have a version number attached, like this:

  • libXfixes.so.3
  • libXrender.so.1
  • libasound.so.2

因此,尽管lib显然存在,但尝试在Ubuntu上尝试打开"libXfixes.so"将失败. 它只是附带了一个版本号.那么我的应用程序应该如何处理呢?
我应该让我的应用程序首先手动扫描/usr/lib/来查看我们拥有哪些库,然后选择一个合适的库吗?还是有人有更好的主意?

So trying to open "libXfixes.so" would fail on Ubuntu, although the lib is obviously there. It just has a version number attached. So how should my app handle this?
Should I let my app scan /usr/lib/ first manually to see which libs we have and then choose an appropriate one? Or does anyone have a better idea?

谢谢大家,

安迪

推荐答案

您应该使用库的SONAME进行dlopen.您可以使用readelf -d [libname]看到它.

You should dlopen using the library's SONAME. You can see that by using readelf -d [libname].

例如,在我的一台Fedora Linux机器上,C库的SONAME是libc.so.6.

For example, on one of my Fedora Linux machines the SONAME of the C library is libc.so.6.

不能保证从.so名称到.so.6名称的符号链接.这些符号链接仅用于编译软件,通常不安装在没有开发包的系统上.

The symlinks from the .so names to the .so.6 names are not guaranteed. Those symlinks are only needed for compiling software and are usually not installed on systems without the development packages.

无论如何,您都不希望最终加载一个具有不同编号的版本,因为编号更改表明API存在重大差异.

You would not want to end up loading a version with a different number anyway, because the number changes indicate major API differences.

这篇关于使用dlopen()动态加载共享对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆