Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 1

Qt-interest Archive, January 2003
Plugin architecture


Message 1 in thread

Not really a Qt specific question, just wondering if anyone knows much about 
writing applications with a plugin architecture? I've been searching for some 
articles on the subject, or a book, but I have had no luck. Have any of you?

Happy hacking,
Martin


Message 2 in thread

On Wed, 01 Jan 2003 23:56:10 +0000, Martin Galpin wrote:

> Not really a Qt specific question, just wondering if anyone knows much about 
> writing applications with a plugin architecture? I've been searching for some 
> articles on the subject, or a book, but I have had no luck. Have any of you?
> 
> Happy hacking,
> Martin


Plugins are cool and very easy to impliment - but you did not state if you
are using Linux or Windblows. I'll assume Linux for the mo...

1) Creating a plugin:

#include <stdio.h>
using namespace std;

// plugin function
extern "C" int function()
{
  fprintf( stderr, "K, Plugin working.\n" );
  return 0;
}

compile using: 
g++ -fPIC -c code.c ... This will create code.o.
then...
g++ -shared -Wl,-soname,plugin.so -o plugin.so code.o ... This will create
plugin.so, which is actual plugin. i.e. the plugin that your program will
load and execute.



2) Calling plugin

#include <stdio.h>
#include <dlfcn.h> // Need this for dl functions

int main() {

    char *(*entry)();  // Pointer to function from Plugin
    void * module;     // Pointer to Plugin
    
    // load the module (plugin.so), and resolve symbols now
    module = dlopen( "plugin.so", RTLD_NOW );
    if(!module)  {
        fprintf(stderr, "[error]: dlopen() failed while resolving symbols.\n" );
        return -1;
    }

    // retrieve address of entry point (the plugin function - function())
    entry = (char*(*)())dlsym(module, "function");
    if( entry == NULL ) {
        dlclose(module);
        fprintf(stderr, "[error]: dlerror() failed while  retrieving address.\n" ); 
        return -1;
    }

    // Call Plugin function
    entry();

    return 0;
}

compile using:
g++ -o driver driver.c -ldl ... -ldl loads the necessary libraries


And that it!

Mike


Message 3 in thread

Also, if you want your code to work in Windows too ( as the code I wrote
will compile with QT obviously as it is C/C++ compatable :-) ), you can
use a dll wraper.

Later, Mike


Message 4 in thread

"Michael Cowan" <michael_cowan@phreaker.net> wrote:

> Also, if you want your code to work in Windows too ( as the code I wrote
> will compile with QT obviously as it is C/C++ compatable :-) ), you can
> use a dll wraper.

...like using QLibrary