Using dll to develop projects is very suitable for team cooperation to develop large-scale projects. It is easy to expand. The libraries are relatively independent, can be compiled independently, and can also protect the source code.
The dynamic link library dll created by Qt in this article is called driver. This section demonstrates how to create a dynamic link library, call it, and finally realize the adder on the UI.
To create a project, select "other project" – > "subdirectory project",
Create a name and select an address
Instead of adding sub items, select cancel
Generated The pro file is as shown in the figure:
TEMPLATE = subdirs //Represents the generation of a subdirectory project
Add a sub project, right-click Plugin and select "new sub project"
After opening, select "Application" – > "Qt Widgets Application"
Add the subproject "MainApp" to "Plugin.pro"
Click "finish" and MainApp will be displayed The contents in pro file are as follows:
Plugin. The contents in Pro are as follows:
You can see that with SUBDIRS, the MainApp in it is plugin Subproject of pro.
//MainApp is plugin Subprojects of Pro SUBDIRS += \ MainApp
Multi core compilation settings: Qt uses one core operation by default when compiling. Some large projects run very slowly, so multi-core compilation can be set in the project. The following figure shows the compilation diagram set to 4 cores.
Compile MainApp subprojects
In the compiled MainApp, the address setting is a little messy. You still need to pay attention to it in the later setting. It will not be changed here:
Add a library plug-in:
Select Library – > C + + Library
Select shared library
Select modules as needed. Only "QtCore" is selected here
MathPlugin. The internal of Pro is as follows:
You can see that the "mathplugin_global.h" file is automatically generated, as shown in the following figure:
Plugin. The MathPlugin plug-in is inserted into pro
At mathplugin h and Set addition function in cpp: note here that in The function is declared in the h file, which is automatically displayed in the The shortcut key for generating function implementation in cpp is Alt+Enter
#ifndef MATHPLUGIN_H #define MATHPLUGIN_H #include "mathplugin_global.h" class MATHPLUGINSHARED_EXPORT MathPlugin { public: MathPlugin(); ~MathPlugin(); double add(double d1,double d2); }; #endif // MATHPLUGIN_H
MATHPLUGINSHARED_ Jump to export to see that it defines # define mathpluginshared for macros_ EXPORT Q_DECL_EXPORT,Q_ DECL_ Export is a macro defined by QT that can be referenced by external modules. Its internal is # define Q_DECL_EXPORT __declspec(dllexport) where__ declspec(dllexport) is a macro definition of libraries defined in standard C + + that can be referenced externally. If you need to write QT code in C + + form, you can use this.
Add the corresponding "lineadit", "pushbutton" and "label" in the UI, and select "go to slot" on the pushbutton
After the interface is created, how to use the plug-in MathPlugin for calculation?
Right click MainApp and select add library
Select internal library
Select the library file "MathPlugin" inserted in MainApp, select the platform as "Windows", and then "next"
Then finish
MainApp.pro will add the contents in the following red box
win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../MathPlugin/release/ -lMathPlugin else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../MathPlugin/debug/ -lMathPlugin
Indicate the paths in release and debug modes
INCLUDEPATH += $$PWD/../MathPlugin DEPENDPATH += $$PWD/../MathPlugin
Add working directory to application
mainwindow. Add #include "mathplugin.h" and implementation code to CPP:
#include "mainwindow.h" #include "ui_mainwindow.h" #include "mathplugin.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_add_clicked() { MathPlugin math; ui->lineEdit_add=math.add(QString::number(ui->lineEdit_d1->text(),ui->lineEdit_d2->text())); }
If #include "mathplugin.h" is added, the display cannot be found. You can compile the two sub projects separately to solve the problem.
Build as a whole and debug runs.
Realize the function interface of an adder (my program runs and reports errors, but the whole process is no problem)
Tips for normal operation:
My error message:
8:07:21: For project Plugin Perform steps ... 18:07:21: Starting "C:\Qt\Qt5.9.4\5.9.4\msvc2017_64\bin\qmake.exe" D:\SelfProgram\QTTest\Plugin\MathPlugin\MathPlugin.pro -spec win32-msvc "CONFIG+=debug" "CONFIG+=qml_debug" 18:07:21: process"C:\Qt\Qt5.9.4\5.9.4\msvc2017_64\bin\qmake.exe"Normal exit. 18:07:21: Starting "C:\Qt\Qt5.9.4\Tools\QtCreator\bin\jom.exe" qmake_all jom 1.1.2 - empower your cores 18:07:22: process"C:\Qt\Qt5.9.4\Tools\QtCreator\bin\jom.exe"Normal exit. 18:07:22: Starting "C:\Qt\Qt5.9.4\Tools\QtCreator\bin\jom.exe" -r -j4 -f Makefile.Debug '-f' It is not an internal or external command, nor is it a runnable program Or batch file. jom: D:\SelfProgram\QTTest\Plugin\MathPlugin\Makefile [debug] Error 1 18:07:22: process"C:\Qt\Qt5.9.4\Tools\QtCreator\bin\jom.exe"Exit, exit code 2. Error while building/deploying project Plugin (kit: Desktop Qt 5.9.4 MSVC2017 64bit) When executing step "Make" 18:07:22: Elapsed time: 00:01.
When you have time, study what's going on