PDA

View Full Version : QPluginLoader doesn't load dynamic plugin when accessing a static plugin



fulalas
6th July 2017, 04:12
I'm playing with Qt Plugins and it's working fine so far. I can load a dynamic plugin (config += plugin) and access its methods from the main application without any problem.

However, after creating another plugin, this time a static one (config += static) linked to the main, and trying to access anything of it from my first dynamic plugin, the dynamic plugin isn't recognized/loaded by QPluginLoader anymore.

So here's my question: is it possible to access a static plugin from a dynamic plugin?

high_flyer
6th July 2017, 11:37
A static plugin is a kind of oxymoron.
If it is static, its not a plugin, since it has to be linked IN to your application, so your application executable has it always in it, which is not what a plugin is, a plugin is optional, and CAN be used, but doesn't have to.
So EITHER its a plugin, OR a static lib.
A static plugin makes no sense.

fulalas
6th July 2017, 12:44
It's a static lib though (... (http://doc.qt.io/qt-5/plugins-howto.html#static-plugins)). Anyone knows the answer of my question? Thanks!

high_flyer
6th July 2017, 13:33
It's a static lib though
Which is what I said:

So EITHER its a plugin, OR a static lib.


Anyone knows the answer of my question?
Well what is the question?
It simply is not clear:
On one hand you say:

The dynamic plugin isn't recognized/loaded by QPluginLoader anymore.
So, if the plugin is not loaded no wonder it can't do anything.
It has nothing to do (yet) with anything else such as accessing your static lib.

On the other hand you ask this:

is it possible to access a static plugin from a dynamic plugin?
And again, the question makes no sense.

Its like asking: "is it possible to be sick while healthy".
Can you answer that?

The dynamic plugin has no linking information about the static lib your application is linking against, so the very simple answer is "no".
However there might be a way to allow this.
Once you link to a static lib, you can see it as an integral part of your application.
If you wish your application to allow access of the functionality it has from the static lib to the plugin, you could create wrapper methods that your plugin interface is can access, and in the implementation of these your application will call the static lib functions.

fulalas
7th July 2017, 05:22
@high_flyer, I don't think we need to go this way (although Qt Plugin page talks a lot about static plugins (http://doc.qt.io/qt-5/plugins-howto.html#static-plugins)). The main point is not that. Let's try another way. :)

My architecture is something like this:

Core -> static lib
MainWindow -> application
Plugin A -> dynamic plugin

Core is statically linked to main, and Plugin A is loaded dynamically by Core. My question is: since Core is known by MainWindow (they share let's say the same memory namespace), then Plugin A can see a method of Core? I'm asking this because when I try to create an instance of Core inside this Plugin A, although everything compiles and links without any problem, Plugin A stops to be recognized as valid plugin by QPluginLoader (i.e. instance() returns a null pointer).

high_flyer
7th July 2017, 12:04
I don't think we need to go this way (although Qt Plugin page talks a lot about static plugins).
Yes, the the essence of that text is that you get the plugin IN Qt.
And since your application links to Qt you get the functionality from these plugins.

This is not your setup however.
Here your dynamic plugin takes the role of the application from the above setup.
Your dynamic plugin is not linking against the application or the static plugin, (like the application linked to Qt above) thus it has no knowledge of the symbols in the static plugin/lib.

And again, the simple answer is "no".
There are ways to allow this, one I mentioned in the previous post.
You can't go around the fact that your dynamic plugin needs to know the symbols of your static plugin/lib.
Or otherwise put: whoever access the static libs functionality needs to know the symbols in it.

BTW - this whole problem is an indicator to a design problem on several accounts.
One, it seems the functionality encapsulation is not correct - as you need functionality from your static lib both in your app and your dynamic plugin.
Which brings the question - why are you using static linking for one of the plugins?
Then you have a problem of tight coupling and rigidness, which means you have bad decoupling of code.
Having plugins depend on other plugins simply defeats the concept of plugins.
The correct way to fix your problem would be to rethink the design - I am aware however, that this is not always an option.
But through usage of interfaces and wrappers you could effect the design without changing it, at least to some degree.

fulalas
8th July 2017, 06:14
@high_flyer, I see. Thanks for the tips. I decided to insert a pointer of Core (static lib) into plugins after loading them. Maybe this is not the most beautiful solution, but it's working nice :)

Thanks once again!