Question

I'm trying to create modular application in C++. Everything works fine when linked statically, but I want to achieve plug-in architecture (using dlopen or LoadLibrary).

All base classes are located in host apllication and these classes are extended in plug-in modules. Modules are loaded at run-time.

Application                Module
----------------           -------------------
| BaseClass1   |           | ExtendedClass1  |
| BaseClass2   |           | ExtendedClass2  |
| BaseClass3   |           | ExtendedClass3  |
----------------           -------------------

But when I try to compile module, linker obviously can't find references to BaseClass methods.

Can I somehow tell linker not to link these classes at compile time and let OS to link them on load in run-time? Or should I use different approach and move BaseClasses to some core library and link both the application and module to this core library?

Was it helpful?

Solution

Just like you said, use the core approach. Create a common/core directory, which has all the header files of BaseClass1 to BaseClass3. When you compile host application, it will have access to this common directory. When you compile plug-in, it will also have access to this common directory.
If you need to release host binary to external customers, you will also include the common directory, so that external customer can program their own plug-in. This is how we do it. Not the best solution, but it works.

OTHER TIPS

The better approach for this is to have the base class in a core library which is usable both by the Application and the Module. However, please note that dlopenand LoadLibrary both can load only functions, so possibly you will need to have a function in your plugin library which will create your desired object.

If you do want to implement a plug-in architecture, with dlopen, have a look at this answer C++ Dynamic Shared Library on Linux.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top