Two versions of same library in one app

Viewed 1347

I have two versions (with almost identical API) of same shared library. I must use them in same application. I know how to resolve name clashes in the headers -- I will just import them like this

namespace version1 {
    #include "version1/library.h"
}

namespace version2 {
    #include "version2/library.h"
}

I do not know though how I resolve linking clashes -- library is dynamically linked. First version creates following structure in custom lib folder:

libsomething.so -> libsomething.so.2
libsomething.so.2 -> libsomething.so.0.8.31.1
libsomething.so.0.8.31.1

second one:

libsomething.so -> libsomething.so.2
libsomething.so.2 -> libsomething.so.0.8.32
libsomething.so.0.8.32

My target environment is Linux machine.

Context:

I have source of libraries but they are downloaded and compiled automatically so I would like to avoid changing anything in CMakeProject of those libs but I can do it if other options are to complicated. I would prefer solution that does changes to those libs after compilation e.g. change soname. But again if I have to do some complicated stuff in app code to use those libs I prefer doing changes to these libs project and maintaining them (because version can change but there will always be two of them -- one version will probably be set forever and never updated so I would prefer to change this non changing version).

1 Answers

My target environment is Linux machine.

It appears that both versions of the libraries, based on the symlinks, have the same SONAME, namely libsomething.so.2.

This completely precludes linking them to the same executable, and loading both of them at runtime using Linux's runtime loader. It does not have this capability.

The only option I see is for your executable to load each library manually, using the dlopen(3) library call. Afterwards you will need to use dlsym(3) or dlsymv3(3) to find the address of each function, in each loaded library, and invoke it indirectly.

Note that dlsym() and dlsymv3() take the mangled version of each function name. It's going to be up to you to figure out what the mangled names of each function are.

Nobody claimed that this is going to be easy, but this is doable when there are absolutely no other options, whatsoever.

Related