Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
217 views
in Technique[技术] by (71.8m points)

c++ - How do plugin systems work?

I'm working on a project where I would find a basic plugin system useful. Essentially, I create the base class and can provide this base class to a plugin developer. Then the developer overrides it and overrides the methods. Then this is where it becomes a bit unclear to me. How does it work from here? Where could I find documentation pertaining to the development of this type of system?

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The plugin systems I know of all use dynamic libraries. Basically, you need to define a small, effective handshake between the system kernel and the plugins. Since there is no C++ ABI, plugins must either only use a C API or use the very same compiler (and probably compiler version) as the system's kernel.

The simplest thinkable protocol would be a function, that all plugin developers would have to provide, which returns an instance of a class derived from your base class, returned as a base class pointer. (The extern "C" makes sure that function won't have a mangled name, and is thus easier to find by its name.) Something like:

extern "C" {
  plugin_base* get_plugin();
}

The kernel would then try to load binaries found in designated places as dynamic libraries and attempt to find the get_plugin() function. If it succeeds, it calls this function and ends up with a loaded plugin instance.

Of course, it would be nice to also have functions to check the version of the API the plugin was compiled against vs. the version of the kernel. (You might change that base class, after all.) And you might have other functions, which return information about the plugin (or you have this as virtuals in the base class). That depends a lot on the nature of your system.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...