I have two shared libraries in Linux that when built are identical in all ways except that the one function that each exposes has a different interface (one takes in some 3rd-party array types and the other takes in strings). I can build both shared library versions, and I was wondering if there was a way that I can insert meta-information into the shared library itself (much like how you are able to insert Comments/Company Name/etc at build time into a DLL, which can then be queried for at runtime).
I do not want to name the two flavors of .so files differently, because they really are the same thing and they are interchangeable pieces of a larger system. I need my code that does a dlopen and dlsym on the library to know which flavor it is so that it has the correct interface.
The kludgy way I am currently accomplishing this is by adding a no-op function called “Flavor_A” in one and “Flavor_B” in the other. Then, I dlsym Flavor_A or Flavor_B to verify which shared library I am dealing with. If I get a NULL back I know Flavor_X doesn’t exist, in which case it must be the other flavor.
I figure there has to be a better way. So, that’s why I’m here.
Anyone have suggestions on how to build in some runtime-queryable information into the .so binary itself?
Thanks.
Instead of having two different symbols, I’d recommend having one symbol that is always there and can return the correct info.
You could also directly export a variable the has the correct flavor;
For the latter, you would access the variable like this: