Search code examples
c++linuxdlopen

Check compatibility of dynamic library at runtime


I am developing a C++ application which is required to load a dynamic library at runtime using dlopen. This library generally won't be written by me.

What method do people recommend to ensure future binary compatibility between this library and my application?

The options as I see them are:

  1. Put the version number in the library file name, and attempt to load it (through a symbolic link) no matter what. If dlopen fails, report an error.

  2. Maintain a second interface which returns an version number. However, if this interface changes for some reason, we run into the same problems as before.

Are there any other options?


Solution

  • You should define a convention about the dynamically loaded (i.e. dlopen-ed) library.

    You might have the convention that the library is required to provide a const char mylib_version_str[]; symbol which gives the version of the API etc etc. Of course you could have your own preprocessor tricks to help about this.

    For your inspiration, you might look at what GCC requires from its plugins (e.g. the plugin_is_GPL_compatible symbol).

    If the dynamically loaded library is in C++, you might use demangling to check the signature of functions....