PDA

View Full Version : Dynamically loading libraries



januszmk
3rd February 2013, 17:22
Hi.
Is there way in QT5 to load libraries dynamically by other library?
I want to do something like that:
My program is loading main library and that library have access to other libraries. I don't want to link them all in my program, because when I want to add some others libs, then I have to release new version of my program. I want to release just new version of main library.
Is it possible to do so?

Santosh Reddy
3rd February 2013, 17:35
Is there way in QT5 to load libraries dynamically by other library?
Yes you can use QLibrary to load shared libraries at runtime. And it is assumed that you main library is build on Qt.

januszmk
4th February 2013, 04:07
I read about libraries and plugins, and I see that if I want to import whole class, I need to declare my library as plugin. So i did something like this:
my lib:


class MAILSYSTEMSHARED_EXPORT MainlLib : public QObject
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QDummyPlugin")
public:
MainLib(QString variable);
}
loading in main.cpp:

QPluginLoader *loader = new QPluginLoader("Mailboxes/lib/libMainLib.so");
QObject *object = loader->instance();
MainLib *lib = qobject_cast<MainLib *>(object);

Now if I have parameters in MainLib, I get no matching function for call to MainLib::MainLib(), candidate is MainLib::MainLib(QString, QString, QString), in file moc_MainLib.cpp
When I remove parameters, in main program I get: undefined reference to `MainLib::staticMetaObject'
What should I do?
Thanks

wysota
4th February 2013, 08:33
Plugin classes require a default constructor.

januszmk
4th February 2013, 14:55
Could you tell me, how can I resolve symbol for class? what symbol should I use?

Edit: ok, I found solution, thanks

wysota
4th February 2013, 16:44
Basically your problem is that you are trying to put class A into a plugin but still make it known to the main program. This is not going to work if the class has the Q_OBJECT macro set. You need a common interface for all your classes that you want to load dynamically into the main application.