PDA

View Full Version : symbol lookup error when using plugins



macbeth
28th July 2006, 23:57
Hello!

I'm trying to make an application which should be extensible using (lower-level, as guys from Trolltech call it) plugins.

I want to access the methods of my own classes in the plugin, which is a source of my problems. I can access methods of Qt classes in a plugin (if they are passed as a parameter), but I can not use methods of my class (although it is passed as a parameter, too).

When I found out that sth is wrong with the plugin system, I tried to modify the Plug&Paint example (to make sure if I didn't mess up sth with plugins), but that didn't work neither.

so here is what I have in a header file:


class myClass : public QObject {
Q_OBJECT
public:
int sayHello(int a);
};


and the in a cpp file:

#include "myClass.h"

int myClass::sayHello(int a){
a += 30;
return a;
}


I'm using slightly modified interfaces.h:


...
class FilterInterface
{
public:
virtual ~FilterInterface() {}

virtual QStringList filters() const = 0;
--> virtual QImage filterImage(const QString &filter, const QImage &image, myClass &cla, QWidget *parent) = 0;
};
...


and in the plugin code (basictools.cpp):

QImage BasicToolsPlugin::filterImage(const QString &filter, const QImage &image, myClass &cla, QWidget * /* parent */)
{ int b = 50;
--> int a = cla.sayHello( b );
QImage result = image.convertToFormat(QImage::Format_RGB32);


Everything compiles fine, but I get an error at runtime (when invoking a filter):

symbol lookup error: /home/.../bin/plugins/libpnp_basictools_debug.so: undefined symbol: _ZN7myClass8sayHelloEi

I've looked up here on the forum, but I didn't find a topic about it, so could you please help me or just a post a link where sth like this is explained?

Thanx.
PS: I think it is the same problem as had a guy here (http://lists.trolltech.com/qt-interest/2006-02/thread00783-0.html), but nobody replied to him...:/
PS2: Compiling on Slack 10.2 with gcc 3.3.6 and Qt 4.1.3

wysota
29th July 2006, 00:23
Where is "myClass" defined (I mean the body of the class)? In the main application or in the plugin? If it is defined in main application, then you have just found your problem. Plugins (by default) can't resolve symbols by looking them up in the main app. The app has to export them explicitly so that the plugin can find them. There are two ways of doing that -- fast or proper...

1. Fast method (tested only under Linux/GCC, should work under Unices/GCC, doesn't work under Win32/MinGW)
GCC has a switch to enable exporting symbols for dynamic linkage. You have to pass "-rdynamic" flag to the linker. You can achieve that by setting "QMAKE_LFLAGS += -rdynamic" in your project file

2. Proper method (works everywhere and is used in 90% of all cases)
Extract all the symbols (classes, functions) from the main application which are to be made available to the plugins and create a standalone shared library (using TEMPLATE = lib) and then link both the application and plugins agains it (LIBS += -lnameoflib)

There is also a third method, very crude but effective if the number of symbols needed by the plugin is very small -- simply copy the sources of those symbols from the main application to your plugin and compile them into the plugin statically.

macbeth
29th July 2006, 08:40
To answer the question, yes, the myClass is defined in a main application. So you gave me the solution, thanks a lot. :)
I've just tried the -rdynamic flag and it works, the second way--I'm sure--also will.

I just wanted to know what is the 'standard' of doing that...

Thanks...

wysota
29th July 2006, 09:54
I just wanted to know what is the 'standard' of doing that...

The standard is deploying a library with symbols that need to be exported.