Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing A Plugin System in C or C++ [closed]

Tags:

c++

c

plugins

What are your tips on implementing a plugin style system?

like image 470
ixo Avatar asked Apr 02 '09 06:04

ixo


People also ask

What is a plugin in C?

A plug-in is a software component that can be loaded by a host application at run-time or during startup. The plug-in provides additional features to the application. However, a plug-in also needs to have some access to the host application in order to make the application features customizable using plug-ins.

How do plugin systems work?

Plugins allow you to write subprograms that then hook into or are attached to a larger program. These subprograms then run, modifying or adding to the behavior of the running program. In order to write a plugin, the program itself has to be written (or hacked) to support the plugin.


4 Answers

In C (and I think C++ too although I haven't done it myself), this is most typically done using dynamically loaded modules. The API:s for such are platform-dependent.

On POSIX (Linux), you use the dlopen() family of functions. Basically you build your plugin separately, then load it at run-time, look up its symbols by name, and can then call them.

For Win32, there is LoadLibrary() which does something very similar, you build your code into a DLL.

For a handy wrapper that makes all of these easy and transparent, check out GLib's GModule API.

like image 74
unwind Avatar answered Oct 14 '22 02:10

unwind


In the '92/'93 time frame I worked on a plugin architecture for Aldus PageMaker, which was coded in C++. PageMaker was built on a C++ OOP framework called VAMP, which assisted its portability between Mac OS and Windows.

So we tried to use the features of C++ to build a plugin architecture. This proved to be very problematic for C++ classes due to the so-called brittle base class problem. I proceeded to write a paper that was published in journals and that I presented at OOPSLA '93 in a reflection workshop. I also made contact with Bjarne Stroustrup at a Usenix conference in Portland and proceeded to dialog with him for several months, where he championed the issue of dealing with the brittle base class problem on my behalf. (Alas, other issues were deemed more important at that time.)

Microsoft introduced the COM/DCOM system and for that platform that was looked on as a viable solution to the problem. C++ could be used as an implementation language for COM via abstract classes used to define COM interfaces.

However, these days developers shun away from COM/DCOM.

In contrast, NeXT devised a plugin architecture using Objective C in the early 90s in the NeXT Step framework. Today that lives on vibrantly in Mac OS X on Apple's computers and important platforms such as the iPhone.

I submit Objective C enabled solving the plugin problem in a superior manner.

I personally regard the brittle base class problem of C++ to be it's most fatal flaw.

If were building a plugin architecture with the C-based family of languages, would do so using Objective C.

like image 22
RogerV Avatar answered Oct 14 '22 02:10

RogerV


The best platform and language neutral advice I can give is this:

Design your entire app around the plugin SDK.

IMO, a plugin SDK should not be an afterthought. If you design your app to basically be an empty shell which loads plugins, then the core features are implemented in your own SDK, you get the following benefits:

  • High modularity of components, and clear separation of purpose (it kind of forces your architecture to be good)
  • It forces your SDK to be really good
  • It allows other third party developers to make extremely powerful, core-level features as well
  • New developers/hires can easily start work on a major new feature without having to touch the main app - they can do all their work in a plugin (which prevents them screwing up anything else)

In C/C++, you probably use dynamic link libraries and either function pointers (C) or interfaces (classes solely consisting of pure virtual methods, for C++). However even if you use Javascript, I'd still recommend the above architecture.

like image 16
AshleysBrain Avatar answered Oct 14 '22 03:10

AshleysBrain


Qt provides QPluginLoader: http://qt-project.org/doc/qt-4.8/qpluginloader.html

If you need/want more fine grained control, Qt also provides a means to load libraries on the fly with QLibrary: http://qt-project.org/doc/qt-4.8/qlibrary.html

Even better, these are portable across platforms.

like image 7
Andrew Ross Avatar answered Oct 14 '22 04:10

Andrew Ross