Linking libraries with incompatible dependecies

故事扮演 提交于 2019-12-05 04:23:53

Since you can't rebuild either of the libraries, and since the libraries can not be allowed to reside in the same "dynamic linker namespace" due to conflicting symbols, your only choice is to isolate them.

You can achieve that by using dlopen("lib*.so", RTLD_LOCAL) (for either or both of the libraries), instead of linking to them directly.

This could be workable if you only need a few symbols from e.g. libfoo.so -- you can simply use dlsym instead of calling the functions directly.

If you have "too many" dependencies on both libraries, your other solution may be to build an "interposer" library. Let's say you want to interpose libbar.so, and you need bar1(), bar2(), ... bar1000() from it.

Write (or generate with a simple Perl script) a source file that looks like this:

static void *handle;
void *bar1()
{
   static void* (*pfn)(void *arg1, void *arg2, void *arg3, ..., argN);
   if (pfn == NULL) {
      if (handle == NULL)
        handle = dlopen("libbar.so", RTLD_LOCAL|RTLD_LAZY);
      pfn = dlsym(handle, "bar1");
   }
   return (*pfn)(arg1, arg2, ..., argN);
}
... repeat for all other libbar functions you depend on

Now compile and link this source into libbar_interposer.so and link your application against it (this will not work for C++ due to name mangling, only for plain-C). Voila, no source changes to the application, and you still have isolated libbar.so so its symbols will not be visible to the rest of the application, and in particular will not conflict with any symbols in libpng.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!