Cross platform plugin library

I need a Cross-Platform, easy to use plugin library.
It needs to be able to load share libraries for Windows (.dll) and Linux (.so I think). The only one I could find was DynObj, but I can't figure out how it works. The compiler I'm using is MinGW.

If any one has used DynObj or another plugin library, I would greatly appreciate an explination on how the library and plugins work.

Thanks in advance!
closed account (S6k9GNh0)
It's easy, across platforms even, to implement your own (although I wouldn't recommend it for use, just as a learning exercise). Each platform provides a similar APIs with possible extended feature for their own implementation of the shared library. For instance, a Windows DLL API adds functionality to treat a DLL as an executable. The POSIX API does not (and the general consensus is that it's silly). However, a Linux shared library can still be used as an executable if so desired, it's just not expected by the POSIX API (example of this is to execute "/usr/lib/libc-<version>.so". It will provide information on extensions and such).

Basically, it's like this:

1. Open DLL/SO, given various parameters.

POSIX uses dlopen(), generally with (RTDL_NOW | RTDL_LOCAL): http://pubs.opengroup.org/onlinepubs/000095399/functions/dlopen.html

Windows uses LoadLibrary(), it provides sane defaults: http://msdn.microsoft.com/en-us/library/ms684179(v=vs.85).ASPX

2. Query a function handle name. It is queried by symbol name as that's how its identified in the shared library.

POSIX uses dlsym(): http://pubs.opengroup.org/onlinepubs/000095399/functions/dlsym.html

Windows uses GetProcAddress(): http://msdn.microsoft.com/en-us/library/ms683212(v=vs.85).aspx

3. After you're done using all functions, close the library.

POSIX uses dlclose() http://pubs.opengroup.org/onlinepubs/000095399/functions/dlclose.html

Windows uses FreeLibrary() http://msdn.microsoft.com/en-us/library/ms683152(v=vs.85).aspx


Notes:
There are various libraries that provide functionality for this already. Boost has a plugin library in sandbox. SDL has one in its core library. GLFW has one for OpenGL although it can be used for anything. Almost every utility library provides one... just understand that one generally does not have extra functionality over the other because of how easy and simple the concept of shared library loading is.
Last edited on
Thank you so much computerquip!

I have a few more questions; can I load classes from shared libraries, does dlopen() work on Angstrom (The default Linux OS for BeagleBones), and what exactly is POSIX?

Note:
I'm new to writing software for Linux.
Linux is (mostly) POSIX:
http://en.wikipedia.org/wiki/POSIX
closed account (S6k9GNh0)
*BSD (including Mac OSX), Linux, and even in Windows using the POSIX subsystem provided by INTERIX ( http://en.wikipedia.org/wiki/Interix ).
Last edited on
I just got it to return a class!

This is the code for the host application

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <stdio.h>
#include <stdint.h>
#ifdef COMP_LINUX
#include <dlfcn.h>
#else
#include <windows.h>
#endif
#include "LibLoader.h"
typedef void* (*libraryFunc)();

int main()
{
    BasePlug* plugin;
    printf("Plugin Test started\n");
    libraryFunc test;
#ifdef COMP_LINUX
//Nothing for linux yet
#else
    HINSTANCE hdll = NULL;
    hdll = LoadLibrary("libTestPlugin.dll");// load the dll
    test = (libraryFunc)(GetProcAddress( hdll, "CreatePluginClassInstance" ));
    plugin = static_cast< BasePlug* > ( test() );	// get pointer to object
    plugin->printStuff();
    delete plugin;
    plugin = NULL;
    FreeLibrary(hdll);				// free the dll
#endif
    return 0;
}


BasePlug is the class all the plugins inherit.

Thanks to all that helped!
Last edited on
Topic archived. No new replies allowed.