<p>Plug-in systems in C++ are hard because the ABI is not properly defined, and each compiler (or version thereof) follows its own rules. However, COM on Windows shows that it's  possible to create a minimal plug-in system that allows programmers with different compilers to create plug-ins for a host application using a simple interface.</p>

<p>Let's be practical, and leave the C++ standard, which is not very helpful in this respect, aside for a minute. If I want to write an app for Windows and Mac (and optionally Linux) that supports C++ plug-ins, and if I want to give plug-in authors a reasonably large choice of compilers (say less than 2 year old versions of Visual C++, GCC or Intel's C++ compiler), what features of C++ could I count on?</p>

<p>Of course, I assume that plug-ins would be written for a specific platform.</p>

<p>Off the top of my head, here are some C++ features I can think of, with what I think is the answer:</p>

<ul>
<li>vtable layout, to use objects through abstract classes? (yes)</li>
<li>built-in types, pointers? (yes)</li>
<li>structs, unions? (yes)</li>
<li>exceptions? (no)</li>
<li>extern "C" functions? (yes)</li>
<li>stdcall non-extern "C" functions with built-in parameter types? (yes)</li>
<li>non-stdcall non-extern "C" functions with user-defined parameter types? (no)</li>
</ul>

<p>I would appreciate any experience you have in that area that you could share. If you know of any moderately successful app that has a C++ plug-in system, that's cool too.</p>

<p>Carl</p>

[{'answer_id': 43325, 'author': 'Serge', 'author_id': 1007, 'author_profile': 'https://Stackoverflow.com/users/1007', 'pm_score': 6, 'selected': True, 'text': '<p>Dr Dobb\'s Journal has an article <a href="http://www.ddj.com/cpp/204202899?cid=RSSfeed_DDJ_Cpp" rel="nofollow noreferrer">Building Your Own Plugin Framework: Part 1</a> which is pretty good reading on the subject. It is the start of a series of articles which covers the architecture, development, and deployment of a C/C++ cross-platform plugin framework.</p>\n'}, {'answer_id': 43373, 'author': 'Jim Buck', 'author_id': 2666, 'author_profile': 'https://Stackoverflow.com/users/2666', 'pm_score': 2, 'selected': False, 'text': "<p>I have my own game engine that has a C++ plug-in system.</p>\n\n<p>I have some code in header files so it gets put into the plugin's compilation unit.</p>\n\n<p>Larger functions that live in the main engine are called via an exported C function (plugin calls MyObject_somefunction(MyObject *obj) which in the engine just calls obj->somefunction()). If calling a C function is ugly for your taste, then with some header trickery, when the header is included in the plugin, have the member function #defined to call the C function:</p>\n\n<pre><code>#if defined(IN_THE_PLUGIN)\nvoid MyObject::somefunction() { MyObject_somefunction(this); }\n#endif\n</code></pre>\n\n<p>Virtual functions either have to be pure or the code lives in the header file. If I'm not inheriting from a class and merely just instancing one, virtual function code can live in the engine, but then the class must export some C functions for creating and destroying the object that is called from the plugin.</p>\n\n<p>Basically, the tricks that I have used, with the goal being to maintain total platform independence, just amount to C exports and header file tricks.</p>\n"}, {'answer_id': 43381, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 3, 'selected': False, 'text': '<p>You might also want to consider replacing the conventional plugin interface by a scripting interface. There are some very good bindings for several scripting languages in C/C++ that have already solved your problem. It might not be a bad idea to build on top of them. For example, have a look at <a href="http://www.boost.org/doc/libs/1_36_0/libs/python/doc/index.html" rel="noreferrer">Boost.Python</a>.</p>\n'}, {'answer_id': 43421, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>I think you are safe creating a plugin system based on:</p>\n\n<ul>\n<li>Packaging of plugin functionality into library (.dll, .so, etc.)</li>\n<li>Requiring that the plugin expose key C-language exports.</li>\n<li>Requiring that the plugin implement (and return a pointer/reference to) an abstract C++ interface.</li>\n</ul>\n\n<p>Probably the most successful C++ plugin system: good old <a href="http://www.photoshoproadmap.com/Photoshop-blog/2006/10/23/programming-photoshop-plugins/" rel="nofollow noreferrer">Adobe Photoshop</a>. And if not that, one of the virtual synth formats such as VSTi etc.</p>\n'}, {'answer_id': 43426, 'author': 'Peter Stuifzand', 'author_id': 1633, 'author_profile': 'https://Stackoverflow.com/users/1633', 'pm_score': 2, 'selected': False, 'text': '<p>The book <a href="http://www.synesis.com.au/publishing/imperfect/cpp/" rel="nofollow noreferrer">Imperfect C++ by Matthew Wilson</a> has a nice info about this.</p>\n\n<p>The advice in the seems to be: as long as you use the same (or equivelant) compiler, you can use C++, otherwise you\'re better of using C as an interface on top of your C++ code. </p>\n'}, {'answer_id': 176893, 'author': 'KeyserSoze', 'author_id': 14116, 'author_profile': 'https://Stackoverflow.com/users/14116', 'pm_score': 3, 'selected': False, 'text': '<p><a href="http://trolltech.com/products" rel="nofollow noreferrer">Qt</a> has a very nice system for plugins that I\'ve used in the past.  It uses Qt\'s meta-object system to overcome many of the problems typically found when trying to develop C++ plugins.</p>\n\n<p>One example is how <a href="http://doc.qt.io/qt-5/qtplugin.html#Q_DECLARE_INTERFACE" rel="nofollow noreferrer"><code>Q_DECLARE_INTERFACE</code></a> works, to prevent you from using an incompatible plugin.  Another is the <a href="http://doc.qt.io/qt-5/plugins-howto.html#the-build-key" rel="nofollow noreferrer">build key</a>, to make sure you load the correct plugin for your architecture, OS, compiler.  If you don\'t use Qt\'s plugin system, these are things you will have to worry about and invent solutions for on your own.  It\'s not necessarily rocket science, and I\'m not saying you\'d fail at it, but the guys at Trolltech are pretty smart and have spent a while thinking about it, and I\'d rather use what they created than reinvent the wheel myself.</p>\n\n<p>Another example is that <a href="http://en.wikipedia.org/wiki/Run-time_type_information" rel="nofollow noreferrer">RTTI </a> typically doesn\'t work across DLL boundaries, but when using Qt, things like <a href="http://doc.trolltech.com/4.4/qobject.html#qobject_cast" rel="nofollow noreferrer">qobject_cast</a> which rely on the meta-object system do work across DLL boundaries.</p>\n'}, {'answer_id': 177079, 'author': 'Martin York', 'author_id': 14065, 'author_profile': 'https://Stackoverflow.com/users/14065', 'pm_score': 2, 'selected': False, 'text': '<p><a href="http://www.cs.wustl.edu/~schmidt/ACE.html" rel="nofollow noreferrer">ACE</a> has a cross platform plug-in architecture.</p>\n\n<p>Check out:</p>\n\n<ol>\n<li><a href="http://www.dre.vanderbilt.edu/Doxygen/5.6.6/html/ace/a00110.html#_details" rel="nofollow noreferrer">ACE DLL</a></li>\n<li><a href="http://www.dre.vanderbilt.edu/Doxygen/5.6.6/html/ace/a00112.html" rel="nofollow noreferrer">ACE DLL Manager</a></li>\n</ol>\n\n<p>I would suggest checking out the book<br>\n<a href="https://rads.stackoverflow.com/amzn/click/com/0201699710" rel="nofollow noreferrer" rel="nofollow noreferrer">The ACE Programmer\'s Guide</a></p>\n'}, {'answer_id': 1286023, 'author': 'Rodrigo Strauss', 'author_id': 57242, 'author_profile': 'https://Stackoverflow.com/users/57242', 'pm_score': 2, 'selected': False, 'text': '<p>Firefox runs on XPCOM (<a href="http://www.mozilla.org/projects/xpcom/" rel="nofollow noreferrer">http://www.mozilla.org/projects/xpcom/</a>). It\'s inspired by Microsoft COM but it\'s multiplatform.</p>\n'}]
['https://Stackoverflow.com/questions/43322', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2095/']