19

I want to write a native application that can be extended with plugins, perferabily in the form of dynamic libraries. I have an idea of what to do, but I would like some ideas, especially best practice tips on what to do and not to do. I worked with similar things on java and php, so I hope I don't bring any bad habits in to my C++.

I'm thinking of allowing developers to implement certain functions like "on_recieve_data(App* app, void* data)" and my application will load all the plugins and call their on_recieve_data function with a pointer to itself (dlsym?).

1
  • 3
    Since you are using C++, why not have an abstract base class, and have a function in each module that returns a pointer to an instance of that class? Instead of using normal functions I mean. Nov 26, 2011 at 6:44

3 Answers 3

16

There are a few things that I consider very important for plugins:

  • Language support

If you want to reach the most number of platforms/languages/compilers then you should write the plugin interface in C and not in C++. The plugin developers can still write their functions in C++, of course, it is just the interface that is C. The problem is that each C++ compiler mangles symbol names in its own way, so if you use C++ you will be forcing plugin developers to use the same compilers and tools that you use. On the other side, there is only one way to export C symbols, so using a C interface for the plugin will allow developers to pick whatever tools they like, and as long as they can produce standard .so/.dll libraries they'll be fine.

  • Memory allocation

In some platforms there are problems when memory allocated by the application is released by a DLL or viceversa. If the plugin has functions that are supposed to allocate memory, then make sure you also require the plugin to provide a corresponding function to release that memory. Likewise, if the plugin can call a function in the application to allocate memory, you should also expose a release function for that memory.

  • Versioning

It is likely that you will have to revise the plugin API after plugins have been written. So your application needs to be prepared to load plugins developed for an older version. You should require an 'init' function in the plugin that the application calls to determine what version of the API the plugin implements and any other information the app might need to know, like the plugin type (if there are different types), what is implemented and what isn't, etc.

Also you have to be very careful when you have to revise the plugin API. You can't change existing functions, since that would break older plugins. Instead you will need to add alternative versions of those functions that have the improvements. Then the problem comes of how to name the new version of an existing function. Typically they'll get the same name plus some suffix ('Ex', a number, etc.). I haven't seen this problem solved in a way that I like.

Likewise, you have to take precautions for structures that are passed between the application and plugins. A common approach is to make the first member of all structures the size of the structure. This works as sort of a versioning mechanism, so that the application can determine what the structure looks like from its size.

Here are a few links that might be of interest:

  • C-Pluff, a general purpose plug-in framework in C (MIT license)
  • lighttpd's plugin.h header file
  • This page has a discussion on how to implement a plugin architecture under Mac OS X, including a short overview of a how to create a C interface for plugins.
  • Blender is an interesting one. The application is written in C++, but plugins are written in Python. Not a bad idea really, it makes it a lot easier for developer to write plugins.

There are plenty of applications written in scripting languages that support plugins (Wordpress, Drupal, Django, and many more). You can look at any of those that are closer to the kind of application you are writing for ideas.

2
  • Thanks for the advice! I did make the decision to use C shortly after making the post. I was looking at some other pluginable applications, namely apache and Wordpress (is Wordpress a good example?).
    – Yifan
    Nov 26, 2011 at 16:37
  • Web browsers are a good source of info regarding plugins. You may also want to look at lighttpd, it is smaller and much simpler than Apache. PHP/Python based CMS can be a good source of ideas as well, though keep in mind that scripting languages have a lot more flexibility than C due to their dynamic nature, so some things you see in scriped plugins will be hard to replicate in C. I have updated my answer with a few interesting links I've found. Nov 26, 2011 at 18:48
1

I believe that this post -> Design Pattern for implementing plugins in your application? does answer your question I guess. It has a lot of refernce for plugin model.

1
  • 1
    Thanks, but the answers on the post seems to be .NET based. I'm looking for more general or at least C based answers.
    – Yifan
    Nov 26, 2011 at 16:40
0

Perhapse, the Eclipse architecture can serve as example: http://www.eclipse.org/articles/Article-Plug-in-architecture/plugin_architecture.html

I'm pretty there is a book from Eclipse's creator, but I can't remember neither the author nor the name of the book.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.