Solved QT5 + DLL loader (run-time)
-
I'm developing a cross-platform x64 (Windows/Linux) software using C++/QT 5 to create a GUI interface (with QT Creator IDE) and Fortran 2003 to deal with all math calculation part.
My goal is develop a closed source-code "main application" that can be extended in a future by plugins that will be created by another developers. These future plugins should be loaded in run-time by the "main application".
Now the question: what is the best way to implement this concept using QT 5?
My problem related with how QT loads static (*.lib or .a files) and dynamic libraries (.dll or *.so files) and what is the "best way" to do this.
Fortran Static Lib + QT Static Linking:
At this moment, I only get all working perfectly when I choose to generate a Fortran Static Lib (.lib) and then link it statically with my QT Project adding this line in the QT project file (*.pro).
LIBS += "full_path_to_my_lib.lib"
In this static linking case, I created a C++ header (*.h) to create the functions reference of my Fortran Lib. This header file contains:
extern "C" { void sum(int *a, int *b, int* c); void sub(int *a, int *b, int* c); }
My Fortran file is like this:
! My sum subrotine: subroutine sum(a, b, c) BIND(C,name = 'sum') !DEC$ ATTRIBUTES DLLEXPORT::sum use, intrinsic :: ISO_C_BINDING implicit none integer (C_INT), intent(in) :: a, b integer (C_INT), intent(out) :: c c = a + b end subroutine sum ! My subtractive subrotine: subroutine sub(a, b, c) BIND(C,name = 'sub') !DEC$ ATTRIBUTES DLLEXPORT::sub use, intrinsic :: ISO_C_BINDING implicit none integer (C_INT), intent(in) :: a, b integer (C_INT), intent(out) :: c c = a - b end subroutine sub
Until this point: All working ok!
PROBLEM: Fortran Dynamic Lib + QT Dynamic Linking:
Reading about how QT can load dynamically all the functions that are inside a generic DLL, I'm confuse about the difference of this 2 QT classes:
1- QPluginLoader: https://doc.qt.io/qt-5/qpluginloader.html
2- QLibrary: https://doc.qt.io/qt-5/qlibrary.html
I would like to know which one I should to choose to my project.
The plugin development part will be done in 2 steps:a) The user interface will be made using C++ and QT
b) All math and calculations will be made using Fortran
So, I think that a new future developed plugin should be a one single DLL file that contains all C++/QT graphical user interface and all Fortran math calculations inside the same DLL file. Right?
So, if the future plugin installer puts this single DLL file inside a pre-defined "Main Application plugins folder"...the main application will find this new plugin and load it automatically at run-time. Right?
Am'I right?
If yes, could you please give a C++/QT example that loads a "simple" DLL file (like my Fortran DLL example above) and how to load all internal DLL functions at run-time.
Finally I saw in QT 5.13 examples 2 QT projects that is very close to my needs:
a) Plug & Paint Example (This is the "Main Application")
Link: https://doc.qt.io/qt-5/qtwidgets-tools-plugandpaint-app-example.html
b) Plug & Paint Basic Tools Example (This is the plugin that loads in "run-time")
Link: https://doc.qt.io/archives/qt-5.10/qtwidgets-tools-plugandpaint-plugins-basictools-example.html
My problem with this QT example is that the "plugin part" is that:
a) The C++ code is not a simple DLL like my Fortran example above and it have other QT classes dependecies that are needed to create a valid QT plugin.
b) The plugin project output file is a .lib (static library file). Why? I my mind was thinking that this should be a *.dll (dynamic library file).
Well, looking in the "main application" example source code, I think that it loads this *.lib file at run-time using QPluginLoader. Does it make any sense?
Until now, I was thinking that:
a) *.lib or *.a => Static Linking at compile time
b) *.dll or *.so => Dynamic Linking at run-time
In what I'm wrong? And how can I load dynamically .dll files in my QT project?
Thank you,
-
Hi and welcome to devnet,
Since you want to build a plugin based application, use QPluginLoader. It allows to easily implement the plugin handling.
As for .lib files, they can either be a full static library or an import library that you use when using dynamic libraries.
dll files are loaded at runtime.
-
@sgaist thanks for your quick response!
Could you please give me a short example about how to call a internal DLL function?
I tried some code like this:
QPluginLoader loader("my_dll.dll"); QObject* plugin = loader.instance(); if (plugin) { // how to call a internal DLL function? }
Inside my *.pro file, I added:
LIBS = -L../plugins
And finally, I copied my dll file to the QT project build folder:
C:\full_path_to_build_folder\plugins\my_dll.dll
Inside my_dll.dll I have 2 functions:
void sum(int *a, int *b, int* c); void sub(int *a, int *b, int* c);
Could you help me to call these functions?
-
I got this code working well. It calls the "sub" function that is inside my DLL file.
int a = 5; int b = 3; int c = 0; // Load library in a cross-platform way: Windows (*.dll), Linux (*.so) or Mac (*.dylib) QLibrary myLib("Dll1"); typedef void (*MyPrototype)(int*, int*, int*); MyPrototype sub = (MyPrototype) myLib.resolve("sub"); if (sub) { sub(&a, &b, &c); } qDebug() << "C = " << c; // Output: c = 2
Question 1:
In the line:MyPrototype sub = (MyPrototype) myLib.resolve("sub");
I got this warning:
Use of old-style cast
How to do a "new-style cast" and remove this warning?
Question 2:
What isMyPrototype
in this code? Why it works?Question 3:
Can I put QT GUI elements (Window, buttons, labels, etc...) inside a DLL file? -
Hi @fem_dev said in QT5 + DLL loader (run-time):
Question 1:
In the line:
MyPrototype sub = (MyPrototype) myLib.resolve("sub");I got this warning:
Use of old-style castHow to do a "new-style cast" and remove this warning?
MyPrototype sub = static_cast<MyPrototype>(myLib.resolve("sub"));
Question 2:
What is MyPrototype in this code? Why it works?You defined it yourself:
typedef void (*MyPrototype)(int*, int*, int*);
. This is just the signature of the function you are calling: avoid
function taking three pointers toint
as parameter. Note that you are defining a pointer to this function, because that is whatmyLib.resolve()
returns.Question 3:
Can I put QT GUI elements (Window, buttons, labels, etc...) inside a DLL file?Yes, but creating a real plugin with
QPluginLoader
is better suited for that.Regards
-
The idea behind plugins is that you create one or more interfaces where you define what your plugin provides. You call then these methods as you need.
-
@aha_1980 thank you for your good explanation.
Please, could you say to me what is the difference between
QPluginLoader
andQLibrary
.
Reading the QT Documentation, I saw thatQPluginLoader
is only for static plugins, but the wierd part is that they are loaded in run-time. I don't understood this very well.In my mind, the word "Static" means a library that will be placed inside the main application binary. And "static" means too that the file extension is ".lib" (Windows) or .a (Linux). Right?
My goal is to build a main application that can load QT plugins at run-time and, of course, they are not inside the main application binary.
So, what the difference about these two approachs:
QPluginLoader
andQLibrary
? -
@fem_dev said in QT5 + DLL loader (run-time):
Reading the QT Documentation, I saw that QPluginLoader is only for static plugins, but the wierd part is that they are loaded in run-time. I don't understood this very well.
AFAIK you can load plugins at runtime. At least they are not linked into your program, but rather external libraries. Qt Plugins are very well suited for Qt code that extends your main program.
In my mind, the word "Static" means a library that will be placed inside the main application binary. And "static" means too that the file extension is ".lib" (Windows) or .a (Linux). Right?
No, the extension is
.dll
or.so
. For example, Qt Creator is fully made of plugins; you can look in thebin
orlib
folder and you will see all the plugins.I use
QLibrary
myself to load external libraries (not written by me). Your Fortran code might be perfect for that.I hope I could clarify that a bit, I suggest that you have a deeper look at the Plug&Paint example and play a bit with it.
-
@aha_1980 thanks for your quick response again and your good clarification about this topic.
Well, I saw the Plug&Paint (the main application part) and Plug&Paint Basic Tools (the Plugins part) QT examples...
The main application is usingQPluginLoader
to load the "Basic Tools" static library plugins. Ok!In this QT example, the Plug&Paint application NOT compiles before compile the "Basic Tools" QT Project. I think that's because the "Basic Tools" output is a static library, and this static library (*.lib) is dependence library of Plug&Paint project.
So, first of all, I need to compile "Basic Tools" project -> so, I get the output static lib file -> and then compile "Plug&Paint" main application.
My goal is different: I would like to develop a QT main application that doesn't have any "plugin dependencies" at compile time.
The plugins must extend the main application only in run-time.My future plugins should be a mix of "Fortran + QT GUI + C++".
Question: 1
Is there a "good practice" to develop this compile-time independent "main application"?
May be examples, tutoriais, etc...Question 2:
What is the best "QT Creator project type" to develop this GUI plugin? Why?
a) QT Plugin?
b) Dynamic Lib?
c) Static Lib?
d) Other?Question 3:
What is the "best way" to load these plugins in run-time? Why?
a) UsingQPluginLoader
?
b) UsingQLibrary
? -
@fem_dev I suggest you to do a bit of information research yourself. As said, playing with existing examples also helps to understand how things work.
Me and @SGaist already gave examples how you could start your work.
Once again: plugins can be compiled separate from your app and can be loaded at runtime. That's what
QPluginLoader
helps you to do.QLibrary
is for loading external libraries, e.g. your pure FORTRAN libs.I really cannot tell you more, because I've used
QLibrary
a lot, but did not much with plugins so far.Regards
-
Hi, to add to @aha_1980: I've used both QLibrary and QPluginLoader, you could say QPluginLoader is a superset of QLibrary. I haven't looked at the source code, but QPluginLoader probably uses QLibrary to load the its plugin .dlls.
They're doing the same basic job, but QPluginLoader does much more checking and tidying up, so wiring up/talking to your .dlls in your app is easier going through QPluginLoader.
IOW, QPluginLoader is more high-level, like an automatic car, while QLibrary is more like driving a stick-shift car :-)
-
Thank you so much @aha_1980 , @SGaist and @hskoglund ....now I'm getting the main idea about
QPluginLoader
andQLibrary
. You guys help me a lot!I will try to do a simple code here.
All the best!
-
@aha_1980 this is the last doubt in this post:
When I try to follow your sugestion replacing this line:
MyPrototype sub = (MyPrototype) myLib.resolve("sub");
To:
MyPrototype sub = static_cast<MyPrototype>(myLib.resolve("sub"));
I got this compilation error:
error: static_cast from 'QFunctionPointer' (aka 'void (*)()') to 'MyPrototype' (aka 'void (*)(int *, int *, int *)') is not allowed
Coul you help me?
Thank you,
-
I've no compiler at hand now, but I thought that should work...
-
What is MyPrototype exactly ?
-
@aha_1980 said in QT5 + DLL loader (run-time):
Hi @fem_dev said in QT5 + DLL loader (run-time):
Question 1:
In the line:
MyPrototype sub = (MyPrototype) myLib.resolve("sub");I got this warning:
Use of old-style castHow to do a "new-style cast" and remove this warning?
MyPrototype sub = static_cast<MyPrototype>(myLib.resolve("sub"));
Question 2:
What is MyPrototype in this code? Why it works?You defined it yourself:
typedef void (*MyPrototype)(int*, int*, int*);
. This is just the signature of the function you are calling: avoid
function taking three pointers toint
as parameter. Note that you are defining a pointer to this function, because that is whatmyLib.resolve()
returns.Question 3:
Can I put QT GUI elements (Window, buttons, labels, etc...) inside a DLL file?Yes, but creating a real plugin with
QPluginLoader
is better suited for that.Regards
-
Please try this:
MyPrototype sub = reinterpret_cast<MyPrototype>(myLib.resolve("sub"));
I'm pretty sure that will work.
-
-