I implemented a very basic “plug-in system” as part of a static library. Each "plug-in" implements support for a specific image format, for example. GIF, JPEG, etc. In addition, I have a Singleton (a class called PluginManager ) that contains a list of all available plugins.
The tricky part is that I want to disable / enable plugins by adding or removing their source files from the project file. To do this, each plug-in creates a global variable (with different names) and registers the plug-in in the constructor of this class before PluginManager .
Something like this for the JPEG format ...
struct JPEGPlugin { // constructor will register plugin JPEGPlugin() { PluginManager::Singleton().RegisterPlugin(this); } // plenty of other code ... }; JPEGPlugin jpeg_instance; // instantiate in global scope
However, while this works theoretically fine, it fails when linking this static library to other code to create an executable. As long as this executable does not gain access to global plugins (for example, jpeg_instance ), the linker does not see the connection (it completely ignores the side effects of the constructor) and does not include the code in the final executable. In other words, the JPEG plug-in is not available in the final application.
I have encountered problems several times over the years, and I have always looked for a network for solutions. Each time I just found pages that basically say that this is a known issue and that I have to live with it.
But maybe someone from SO knows how to make this work?
c ++ plugins static-libraries
beef2k
source share