checking version of C library (dynamic loading)
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 pos开发者_StackOverflowsible 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!
My question is, how can I check which version of a library is installed without using the package manager to do so.
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 or Makefile
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:
void gst_version(guint *major, guint *minor, guint *micro, guint *nano);
Gets the version number of the GStreamer library.
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).
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
Try using ltrace or strace
ltrace -f -e dlopen ./<theapp>
strace ./<theapp> 2>&1| grep 'dlopen.*'
PS! Haven't tested myself
精彩评论