Since the question is quite unclear, I just blindly guess what you are trying to do and give a short explanation of how to do that.
Typically, plugin mechanisms using shared/dynamic libraries are implemented such that you have an interface.
class IPlugin
{
virtual int doSomething() = 0;
};
Next, you implement this interface in your library and, in addition, provide a C function to create an instance of the implementation class (C function, because they are not name-mangled).
class MyPlugin : public IPlugin
{
int doSomething() override { /* ... */ }
};
extern "C"
{
IPlugin* createPlugin()
{
return new MyPlugin{};
}
}
Now, in your app you load the library using dlopen
(or LoadLibrary
on Windows) and fetch the address of your createPlugin
function using dlsym
(or GetProcAddress
):
void* addr = dlsym(handle, "createPlugin");
Then, you cast this to the actual type, which is equivalent in all libraries:
auto createPlugin = reinterpret_cast<IPlugin*(*)()>(addr);
By calling createPlugin
you can now create an instance of your concrete implementation and access it through the interface. There is no need for fetching any additional methods, since virtual
member functions can be called directly across library boundaries (because of the vtable).
IPlugin* plugin = createPlugin();
int result = plugin->doSomething();
Same works for functions with arguments and different return types as well, of course. You should however export another C function to destroy the object instance again. delete
should always be called by the library that created the object (since the library might have been built with a different implementation of new
than the executable).