1

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?

chatoooo
  • 326
  • 1
  • 3
  • 13

3 Answers3

1

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.

Ferenc Deak
  • 34,348
  • 17
  • 99
  • 167
  • Yes, I know about that. I made some experiments and I'm loading function which returns pointer to object instance. I also think that is better to have core library, because I can later change CLI application to GUI whithout moving classes to new one. – chatoooo Mar 24 '14 at 21:05
1

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.

m. c.
  • 867
  • 5
  • 12
0

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

Xavier Lamorlette
  • 1,152
  • 1
  • 12
  • 20