As far as I know, the C ++ compilation model does not have a direct direct way to βexport classesβ. However, you can do this with a simple C interface:
#include "MyModule.h" // class MyModule inherits Module extern "C" Module * create_module() { return new MyModule; } extern "C" void free_module(Module * p) { delete p; }
Now you can dynamically load this library and extract the create_module and free_module and dynamically add their function pointers to your system:
std::map<std::string, std::pair<Module * (*)(), void(*)(Module *)> plugins; plugins["MyClass"] = std::make_pair(..., ...);
In fact, you probably donβt even need the destroyer function, since the usual mechanism of the virtual destructor should work even in dynamically loaded libraries.
For instance:
std::unique_ptr<Module> make_module(std::string const & s) { auto it = plugins.find(s); return { it == plugins.end() ? nullptr : it->second.first() }; }
source share