tags:

views:

753

answers:

5

When developing a C/C++ (=2?) plugin based framework with shared objects/dynamic libraries that need to support live swapping what examples would be helpful to look at for implementation details?

Thanks.

Note: live swapping is the key point here, no need to restart the system is a requirement

+3  A: 

If you are on POSIX, dlopen(), dlsym() and dlclose() are all you need.

See man dlsym for details and examples.

There is a good article about loading dynamic libraries, and plugin infrastructure is an example.

EDIT OP added Windows as requirement so this approach won't help since Windows isn't POSIX-compliant. However there are similar functions in WinAPI - see here.

qrdl
Althoug possible I would recommend to use a platform independent library like ACE http://www.dre.vanderbilt.edu/Doxygen/Current/html/ace/a00110.html
lothar
+1  A: 

You might want to try Boost.Extension but beware : despite its name, it is not one of boost libraries.

Here is a link to its documentation.

Benoît
very niceits not boost(yet) but it's in the sandbox
Robert Gould
+1  A: 

For C++ plugins you can check this article which detail how to achieve it with the previously mentionned posix calls.

Quoting the article :

Given that we can use these functions to access functions in a C library, how do we use them to access classes in a C++ library? There are several problems to overcome. One is that we must be able to locate the symbols we need in the library. This is trickier than it might seem because of the difference between the way symbols are stored in C and C++ files.

fa.
+1  A: 

Boost.Extension seems nice (never used it but will try soon). Another alternative would be the POCO SharedLibrary class.

Klaim
+1  A: 

If you want cross-platform library loading without having to develop for each platform's API individually, libltdl may help.

Libtool provides a small library, called libltdl, that aims at hiding the various difficulties of dlopening libraries from programmers. It consists of a few headers and small C source files that can be distributed with applications that need dlopening functionality. On some platforms, whose dynamic linkers are too limited for a simple implementation of libltdl services, it requires GNU DLD, or it will only emulate dynamic linking with libtool's dlpreopening mechanism.

libltdl supports currently the following dynamic linking mechanisms:

  • dlopen (Solaris, Linux and various BSD flavors)
  • shl_load (HP-UX)
  • LoadLibrary (Win16 and Win32)
  • load_add_on (BeOS)
  • NSAddImage or NSLinkModule (Darwin and Mac OS X)
  • GNU DLD (emulates dynamic linking for static libraries)
  • libtool's dlpreopen (see see Dlpreopening)

Boost.Extension seems to only support Windows PE dlls, UNIX ELF shared objects, and Mac OS X Mach-O bundles. Well, that may be sufficient for you...

ephemient