QML Plugin -- link to C++ library
-
Hi,
I seem to have problems with my QML project structure. I'll break it down to a small example; please ask if you need more details
(everything below refers to Qt 5.8, using MinGW 32bit and MVSC2015 64bit builds):- My QtQuick app MyApp uses a QML module MyModule.
- MyModule depends on a QML plugin MyPlugin.
- MyPlugin depends on a C++ library MyLib.
(All "My..." subprojects are self-written as the names suggest).
When I try to build this in the (for me) obvious way and run MyApp, I get a MyPlugin load failure at program start.
Error message:
qrc:/main.qml:5 plugin cannot be loaded for module "MyModule": Cannot load library [...]\MyPlugin.dll: The specified module could not be found.(Partial) fix to this error:
I can get MyApp to run by additionaly linking to MyLib directly from within MyApp.Problems:
- I do not really understand why I need to do this (MyLib is only used from within MyPlugin).
- "Partial fix" means, I can only start the app from within QtCreator, but now I have problems with deployment:
When I try to start MyApp.exe directly (outside of QtCreator, and after running windeployqt on the build directory), I still get the error that MyPlugin cannot be loaded.
More precisely:
With the MinGW build,
I again get the error as above ("... The specified module could not be found.") When I take the MyLib out of the game (make MyPlugin not dependent on MyLib), starting the app directly works perfectly fine.With the MSVC2015 build, I get an error window:
The application was unable to start correctly (0xc000007b). Click OK to close the application.The latter may be unrelated to the linking problem. According to my google efforts, the error seems to suggest a clash of 64- and 32bit binaries, but
all binaries involved a) have been built in the same subproject and b) work when I run them from within QtCreator.Can anyone help my understand what is going on?
Thanks!
-
MyPlugin.dll load is failing becz it is not able to load the dependent library. Ensure that dependent library is also in path.
Also I'm assuming that your QML App, plugin, lib are compiled using either MingW or MSVC compiler. You can't mix both.
-
Hi!
MyPlugin.dll load is failing becz it is not able to load the dependent library.
Ok. That means it is indeed necessary that MyApp itself links to MyLib?
Ensure that dependent library is also in path.
Adding the lib path to the PATH indeed solves the problem - I can now run the app directly. However, this would mean that deployment of the app would involve changing the user's PATH. Is there no way around this?
A more general question (for my general understanding): What is the difference between a direct program start and a run from within QtCreator (CTRL+R) that makes the latter work without the lib being in the path?
Also I'm assuming that your QML App, plugin, lib are compiled using either MingW or MSVC compiler. You can't mix both.
Yes, this I am aware of.
Thanks for your help!
-
You need not link against the dependent library. It has to be in the path. This is required only during the compilation of plugin. However when you load the plugin, its dependent library also need to be in the path.
When you run from the Qt creator, Qt libraries are already in the path. So program will run.
When you copy the exe standalone, you need to copy all the dependent libraries or use qwindeployment tool.
Hope this clarifies.
-
Thanks @dheerendra, I am starting to understand something. I have some follow-up questions though:
[The dependent library] has to be in the path. This is required only during the compilation of plugin. However when you load the plugin, its dependent library also need to be in the path.
If possible, I would like to avoid this. I sense some potential conflicts when I have e.g. the different builds on my own machine for test reasons (MSVC and MinGW) and start to add several libs to the path. So, questions:
-
Even if I copy the library to the same directory as the plugin, plugin loading fails (if the lib is not in the path). Is there not some default location that a plugin uses to find dependent libs?
-
When looking for a way to setup a suitable "environment" inside the app, QCoreApplication::addLibraryPath came to mind. But it seems I cannot do this inside the plugin as I have no QCoreApplication there. Any way around this? Or am I completely wrong on that track?
Possibly related question: How does QtCreator actually achieve this:
When you run from the Qt creator, Qt libraries are already in the path. So program will run.
?
Hope this clarifies.
Slowly ;-) Thanks again!
-