Using dlopen and dlsym with a third party c++ library

106 Views Asked by At

Newbie question. Suppose there is a third party library ClassLibrary.so, which contains the class definition of a ClassTest and I don't have access to the source of ClassLibrary.so. Can I use ClassTest in my code with dlopen and dlsym? Personally, I would like to compile with ClassLibrary.so but the client wants to use dlopen and dlsym.

I have not tried much as I don't know how to start. I went through this article https://www.linuxjournal.com/article/3687 but it seems that this article requires access to the source of library to create a function that returns a pointer to the class within the source and use this function with dlopen and dlsym.

1

There are 1 best solutions below

0
Employed Russian On

Can I use ClassTest in my code with dlopen and dlsym?

Maybe.

If the ClassLibrary.so exports all the methods of ClassTest and ClassLibrary.h provides a declaration for ClassTest and that declaration actually matches that one used when ClassLibrary.so was built, and you know exact compiler version and flags used to build ClassLibrary.so.

In practice you probably can't know the compiler version (even if strings -a ClassLibrary.so says something like GCC 10.0.1, that's not necessarily sufficient), nor the build flags, and you can't know whether developers of ClassLibrary.so have played any tricks with the declaration of ClassTest (if they even provided such declaration).