QT application programming: QT5 under Window system creates DLL dynamic library and completes the call

1. Environmental introduction

Operating system:  win10 64 bit

QT version:  QT5.12.6

Compiler:  MinGW 32 bit

Second, create a dynamic library

2.1 Ways to create a library 1 

The QtCreate project creation wizard has a project option to create a library. After creating the project in accordance with the wizard, write the code, click on the "Small Hammer" in the lower left corner of QtCreate , the build is completed, and the library file can be generated in the build directory.

 

2.2 Ways to Create a Library 2

Under normal circumstances, the project may have been created before and the functions have been completed, and now I want to generate a library file to be called by others. At this time, only need to modify the xxx.pro project file.

Add the following two lines of code to the original project file.

TEMPLATE = lib

DEFINES += DLL_CREATETEST_LIBRARY

After modification, just build the project. 

 

The dynamic library developed and produced in VS+QT can be directly configured in the project properties of VS:

 

Three, call the dynamic library

 3.1 Create a new project and call the dll library

3.2 Copy library files and header files

(1). Copy all the header files used when generating the library (the original project) to the directory of the new project (the test project that calls the library).

(2). Copy the dll dynamic library generated by the original project to the directory of the new project (the test project that calls the library).

  (3) Modify the xxx.pro project file

  Add the code:

LIBS += $$PWD/DrawTimeLine.dll

 

(4). Modify the main function code and call the test function

#include <QApplication>
#include "widget.h"
#include "drawtimeline.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    return a.exec();
}

 

3.3 Finish calling test

After the build is completed, you also need to copy the dynamic library files used to the directory you just built. Otherwise, the library file cannot be found during runtime.

 

Guess you like

Origin blog.csdn.net/xiaolong1126626497/article/details/112158922