Good patterns for a C/C++ plugin-based system?

2019-01-16 21:21发布

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

标签: c++ c plugins
5条回答
放我归山
2楼-- · 2019-01-16 21:39

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

查看更多
做个烂人
3楼-- · 2019-01-16 21:45

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.

查看更多
来,给爷笑一个
4楼-- · 2019-01-16 21:49

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.

查看更多
淡お忘
5楼-- · 2019-01-16 21:49

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.

查看更多
6楼-- · 2019-01-16 21:50

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...

查看更多
登录 后发表回答