
Originally Posted by
rluceac
Humm... that's it...
I create interfaces to the functions the modules must implement, configure the modules to be load in an text file or XML file, and then load the with dlopen...
Thanks a lot...
Because this is C++, there are a few things to consider... The names of C++ functions are mangled, which makes it hard to use dlsym(). You have to mangle the name the exact same way the compiler did, and that's not always easy.
Thus, public functions in loadable C++ modules are often declared extern "C" to cause them not to be name-mangled. A module for a C++ architecture might provide one or two entry points which are basically factory calls that instantiate module objects.
Suppose that every module inherits from this class:
Code:
class MyModule
{
public:
virtual ~MyModule() {}
virtual void DoIt() = 0;
};
Then a module which implements a "FooModule" might look like this:
Code:
class FooModule : public MyModule
{
public:
void DoIt()
{
// implement the module code
}
};
extern "C"
MyModule *CreateModule()
{
return new FooModule();
}
The main program might load the module like this:
Code:
// Load the module
void *handle = dlopen("FooModule.so", RTLD_LAZY);
// Get the factory
MyModule *(*factory)() = (MyModule *(*)())dlsym(handle, "CreateModule");
// Create the object
MyModule *module = factory();
// Do it!
module->DoIt();
Unloading modules can be tricky, because you can't unload safely unless all object instances from that module are destroyed. You could use some kind of reference counting, or punt the issue and just never unload a module once it has been loaded.