PDA

View Full Version : plugin loading problem



naresh
3rd August 2006, 13:24
Hi there! I'm trying to play with plugins for my app...

here is some code:

plugin interface:

#ifndef PLUGINIF_H
#define PLUGINIF_H

class PluginInterface {
public:
virtual ~PluginInterface() {}
virtual QString version();
virtual QString author();
virtual QString description();
virtual QString name();
};

Q_DECLARE_INTERFACE(PluginInterface, "Qxygen.PluginInterface")

#endif

plugin header:

#ifndef PLUGIN_H
#define PLUGIN_H

#include <QObject>
#include <pluginif.h>

class plugin: public QObject, public PluginInterface {
Q_OBJECT
Q_INTERFACES(PluginInterface)

public:
QString author();
QString version();
QString description();
QString name();
};

#endif

plugin implementation:

#include <QtPlugin>

#include "plugin.h"

QString plugin::author() {
return "Naresh";
}

QString plugin::version() {
return "0.0.1";
}

QString plugin::description() {
return "plugin descr";
}

QString plugin::name() {
return "plugin";
}

Q_EXPORT_PLUGIN2(my_plugin, plugin)

here is plugin loading loop:


QDir dir( QCoreApplication::applicationDirPath()+"/plugins/" );
QStringList fileNames=dir.entryList( QStringList("*.so"), QDir::Files, QDir::Name);

foreach( QString fileName, fileNames ) {
qDebug()<<dir.absoluteFilePath(fileName); // returns proper path everytime
QPluginLoader loader( dir.absoluteFilePath(fileName) );
QObject *plugin=loader.instance();
if ( plugin ) {
PluginInterface *plif=qobject_cast<PluginInterface*>( plugin );
QStringList values;
values<< plif->name() << plif->version();
new QTreeWidgetItem( ui.pluginList, values );
}
qDebug()<<loader.isLoaded(); // always return false
}

project file looks like this:

TEMPLATE = lib
CONFIG += plugin release
INCLUDEPATH += ../interfaces
TARGET = ../../plugins/plugin
VERSION=0.0.1

HEADERS = \
plugin.h

SOURCES = \
plugin.cpp

As you can see in comments... plugin loading loop always return false for isLoaded and never loads plugin... and path it uses always is proper... any ideas?

jacek
3rd August 2006, 14:01
Q_EXPORT_PLUGIN2(my_plugin, plugin)
TARGET = ../../plugins/plugin
These names must match.

naresh
3rd August 2006, 14:23
I've changed those lines to be same but still can't load it...

Kumosan
3rd August 2006, 15:18
The Qt plugin classes are really hard to use. Usually you have no idea why loading fails. I noticed that plugins are extremely sensitive to bugs in the .pro files.

If even one of your .h or .cpp files includes a .h which is not in your .pro file, even if not used at all, plugin loading might fail.

I usually even create a fake .cpp file for each interface file to put it in the .pro file.

jacek
3rd August 2006, 16:05
The Qt plugin classes are really hard to use. Usually you have no idea why loading fails.
Never tried it, but "static plugins" might produce linker errors.

Anyway the problem with the above plugin was that the interface wasn't a real interface.
It should be:

public:
virtual QString author() = 0;
virtual QString version() = 0;
virtual QString description() = 0;
virtual QString name() = 0;

Jeroen van der Waal
9th June 2007, 19:30
Hi,

I am busy trying to figure out what goes wrong. I developed some plugins for the qt designer and sometimes it did not load either. To test if a plugin will load, you can do the following:

create a file called main.cpp with just this:

int main(int argc, char* argv[])
{
return 0;
}

then compile (libfirstplugin.so is the plugin you like to test)

g++ test.cpp libfirstplugin.so

In case of your code, I got:

libfirstplugin.so: undefined reference to `typeinfo for PluginInterface'
libfirstplugin.so: undefined reference to `vtable for PluginInterface'

So this is I guess why the plugin is not loading. I am searching for an answer on these messages

Jeroen van der Waal
9th June 2007, 20:05
Hi,

See attachment, it works now. Maybe you can check if it also works for your application.

Cheers,

Jeroen