Hi, It’s the electrical engineer again, asking complicated questions
Today: Understanding how to handle C++ libraries loaded at runtime.
First: What’s my motivation behind this question: I’d like to check if a certain dynamic C++ library (to be specific: uhd - a library to access some software defined radio hardware) is installed on a system and use it in that case. However, I don’t want to redistribute the whole library with my project, as this functionality should be optional. As far as I know, this library is not intended to be used this way. It offers an alternative pure C interface (which seem to make things a lot easier) for most functionality, but sadly not for all features I need.
Now I did some research on that topic and found this document, explaining how dynamic loading through
dlopen works and why there is the need of factory functions that are declared as
extern "C". I think I understood most parts of it and I believe most of it applies to other platforms too.
Now to gain some better understanding of the possibilities and restrictions I tried creating a little test-case with an Xcode project that builds a small dylib and another JUCE-based command-line project that tries loading und using my libraries functions.
So far I was only successful at using plain C functions in this setup. Problems arise when trying to invoke any of the member-functions on class instances generated by those factory functions, ranging from linker errors in case of simply including the headers for my library in my loading project (I understand them) to runtime errors when declaring a base class with virtual functions, using this header in both projects and creating a derived class in my library. So there are a bunch of questions for me at the moment:
- Can anyone provide a good working example of how to create a simple class that can be used with dynamic loading or point me in the right direction of how this class has to be designed?
- Is a usual C++ library like the one mentioned above even usable in this case?