检查C库的版本(动态加载)
我有一个程序需要特定版本的库(libgstreamer 就是一个例子),因此旧版本将无法工作。由于延迟链接,我的程序可能会链接到 gstreamer 版本 10.23,该版本缺少我使用的 10.25 中存在的一些符号。我的问题是,如何在不使用包管理器的情况下检查安装了哪个版本的库。是否可以从 C 程序加载库并使用 dlopen() 检查其版本号?
编辑:我正在 Linux 系统上工作
编辑 2:也许我可以使用 readelf -V ?
谢谢!
I have a program that requires specific versions of libraries (libgstreamer is an example), and therefore an older version will not work. Due to lazy linking it's possible that my program will link to gstreamer version 10.23 which is missing some of the symbols present in 10.25 that I use. My question is, how can I check which version of a library is installed without using the package manager to do so. Is it possible to load a library from a C program an check its version number using dlopen() perhaps?
Edit: I'm working on a Linux system
Edit 2: Perhaps I can use readelf -V ?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,请注意,在 Linux 上处理此问题的常用且公认的机制正是让包管理器检查它 - 这是它们被发明的主要原因之一。
另一个常见选项是在从源代码构建时签入您的
configure
脚本或Makefile
。如果您想提供预编译的二进制文件,但不使用包管理器,我相信最好的机制是检查库是否有某种内部机制来在运行时检索其版本。不幸的是,这对于您链接的每个库都是特定的。
幸运的是,GStreamer 有这个:
http://gstreamer。 freedesktop.org/data/doc/gstreamer/head/gstreamer/html/gstreamer-GstVersion.html
您可以在启动时调用此函数,如果版本不正常则退出。
尽管如此,我还是建议您重新考虑不使用包管理器。它确实是处理此问题的最轻松的机制,并且还允许用户轻松安装(如果您提供存储库)。
First, please note that the usual and accepted mechanism to handle this on Linux is precisely to let the package manager check it - that's one of the main reasons they were invented.
The other common option is to check in your
configure
script orMakefile
when building from source.If you want to supply precompiled binaries, but not use a package manager, I believe the best mechanism is to check if the library has some internal mechanism to retrieve its version at runtime. This is unfortunately specific for each library you link against.
GStreamer fortunately has this:
http://gstreamer.freedesktop.org/data/doc/gstreamer/head/gstreamer/html/gstreamer-GstVersion.html
You could just call this function on startup, and bail out if the version is not OK.
Still, I'd urge you to reconsider not using a package manager. It really is the most painless mechanism to handle this, and also allows easy installation for users (if you provide a repository).
当然,使用 dlopen 和 dlsym 函数加载库并测试您所依赖的符号是否存在。有关详细信息和代码示例,请参阅手册页: http://linux.die.net/man/ 3/dlopen
Sure, use the dlopen and dlsym functions to load the library and test for the existence of the symbols you are dependent on. See the man page for details and a code example: http://linux.die.net/man/3/dlopen
尝试使用 ltrace 或 strace
PS!没有测试过自己
Try using ltrace or strace
PS! Haven't tested myself