Hot-pluggable C++ library possible?
Asked Answered
E

7

10

I'm looking to "hot plug" a library of C++ code. I'm interested in having this technique work cross platform between Linux/Mac/Windows. Basically I want to have the main program #include "StateMachine.h" which defines all callable interfaces. Then at runtime and DURING EXECUTION load and unload StateMachineLibrary.a to have my application use different state machines.

One thought I have is maybe do something like write a wrapper that loads this compiled code in to my own malloc'd memory and creates function pointers in to that memory?

Motivation is that the State Machine portions of my project will be frequently changing and need recompilation, also would allow the main app to continue running with different State Machines being loaded. I'm hoping to use a "hot-pluggable" library INSTEAD OF something like Lua scripts due to some concerns, so considering that as an alternative has already been explored.

Engird answered 23/6, 2010 at 16:45 Comment(3)
The keyword you're looking for is plugin, not hot-pluggable. Googling c++ and plugin ought to get you some really good hits.Electrodeposit
Related: #384621 and if you need cross-platform plug-ins: #1854823Stephie
Have a search for C++ OSGI - there are several libraries that implement that framework.Copenhaver
H
17

Define a base interface and derive your implementations from it. Put these into dynamic libraries (DLL/SO) and load them at runtime. The library will just need a static factory function to deliver an instance of its implementation to you.

// shared
class Base {
 public:
   virtual void DoTheWork() = 0;
};

// within the DLL/SO
class Hotplugged : public Base {
  public:
   virtual void DoTheWork() {
      std::cout<<"I got hotplugged!"<<std::endl;
   }
};

extern "C" Base* CreateIt() {
  return new Hotplugged();
} 

// within the app (sample for Windows/MSVC)
... ::LoadLibrary("mydll");
Base* (*fpCreateIt)() = (Base*(*)())::GetProcAddress(... "CreateIt");
// call the function pointer to obtain a Base instance
Base* mybase = fpCreateIt();

// prints the above text
mybase->DoTheWork(); 
delete mybase;

Note: this is just a sketch. It has some flaws, for example I'm ignoring ownership semantics, and no actual checks are done if the DLL we just loaded is binary compatible with us. Think about it a bit, or look for existing implementations (some are mentioned in other responses).

Husted answered 23/6, 2010 at 16:47 Comment(0)
D
6

This is possible. For cross-platform work (at least recompile only), you might want to look at some existing frameworks that do this.

OpenSceneGraph includes a full featured, "hot-pluggable" implementation for loading and unloading plugins.

Qt has a plugin framework, as well.

The "trick" is having a clean interface for your plugins, and just using dynamic libraries that can be loaded and unloaded. Nearly every platform (all major ones) support dynamic loading and unloading of libraries, so there is nothing that prevents this from working.

Dhyana answered 23/6, 2010 at 16:52 Comment(0)
T
2

Yes - it's certainly possible. In a previous role where we developed an 3D graphics API and applications we let the user choose the display driver "on the fly". The view had to be recreated, but the application itself didn't need to shut down.

Taste answered 23/6, 2010 at 16:48 Comment(0)
M
2

Though many parts of it are quite dated, Advanced C++ Programming Styles and Idioms (James Coplien) has a section on how to do such things that might be useful to read through (though I'm not sure I'd buy a copy just for this).

Melitta answered 23/6, 2010 at 16:51 Comment(0)
U
2

Check out Boost.Reflection and Boost.Extension - they were designed to address the various issues involved with attempting such things. I'm pretty sure it still doesn't let you work across compilers or versions, but it might be of aid to you.

Unisexual answered 23/6, 2010 at 16:51 Comment(0)
F
1

I wrote v3c-dcom initially just to see if I could do it - you can download it from Sourceforge.
it's basically just a plug-in system at the moment.
It depends on three other SourceForge projects, so you will have to download and install them first.

Go to SourceForge http://sourceforge.net/ and download the following projects:
* v3c
* treedb
* meta-treedb
* v3c-dcom

v3c contains a build system and a general utility library.
treedb contains the core "persistent memory" functionality.
meta-treedb wraps treedb inline implementations in a blanket, shortening compile times and code bloat.
v3c-dcom contains some examples, including creating a plug-in repository inside the program, adding a library to the repository, calling CoCreateInstance() to create objects, and calling methods on those objects.

I designed the build system to be user friendly, even though it's automake based ;)

Just do make && sudo make install in the unpacked directories of each project in turn.

If you're paranoid or don't have "sudo" privileges read v3c's README and the "tryout" script on how to unpack/build/install the packages under a directory you own.

make check will run each library through its paces and for v3c-dcom it will run the demo I mentioned above.

Hope this helps.

Festal answered 27/2, 2011 at 22:16 Comment(1)
thanks for the info and welcome to the site! As an aside and for anyone that cares, I ended up abandoning this plan simply because of limitations on dynamic libraries on iOS which is a primary target of mine. And I also figured that for anything I'm doing I can achieve similar results by static linking and just instancing (same as 'built in' code at that point)Engird
F
0

And don't forget XPCOM. It's designed to be a cross-platform COM.

Fjord answered 23/6, 2010 at 16:56 Comment(1)
Firefox is based on it and a lot of people are using it everyday. Office is based on COM and more people are using it everyday.Fjord

© 2022 - 2024 — McMap. All rights reserved.