Qt generation and calling dynamic library dll, and static libraries .a (windows and linux common)

System 1: ThinkPad T570, Windows10, QT5.12.2 (Qt Creater 4.8.2)
a dynamic creation and library calls .dll
1. qtcreater in creating a dynamic object, dynamic library mydll named as follows:

Select the Library Project, C ++ library Here Insert Picture Description
to select the shared library: Here Insert Picture Description
Select qt comes with the kit:
Here Insert Picture Description
automatically generated in the project mydll.pro file reads as follows:

#-------------------------------------------------
#
 # Project created by QtCreator 2019-04-05T11:14:57
#
#-------------------------------------------------

QT       -= gui #在选择需要的模块时,我只选用了QtCore,没有使用QtGui

TARGET = mydll #我配置的动态库的名字:mydll
TEMPLATE = lib #生成库时该字段为lib;生成执行文件时为:app

DEFINES += MYDLL_LIBRARY #将MYDLL_LIBRARY添加为编译时的预处理器宏,在share_global.h中使用

 # The following define makes your compiler emit warnings if you use
 # any feature of Qt which has been marked as deprecated (the exact warnings
 # depend on your compiler). Please consult the documentation of the
 # deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

 # You can also make your code fail to compile if you use deprecated APIs.
 # In order to do so, uncomment the following line.
 # You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
        mydll.cpp

HEADERS += \
        mydll.h \
        mydll_global.h 

unix {
    target.path = /usr/lib
    INSTALLS += target
}

mydll.h written document reads as follows, of which I only added the sum and squaresum two function definitions, the other is automatically generated:

#ifndef MYDLL_H
#define MYDLL_H
#include "mydll_global.h"
class MYDLLSHARED_EXPORT Mydll
{
public:
    Mydll();
    int sum(int a,int b);
    int squaresum(int a,int b);
};
#endif // MYDLL_H

mydll_global.h file is generated automatically, much described herein.

mydll.cpp file written as follows:

#include "mydll.h"
Mydll::Mydll()
{
}
int Mydll::sum(int a, int b)
{
    return (a+b);
}
int Mydll::squaresum(int a, int b)
{
    return (a*a+b*b);
}

Then press Ctrl + B key to build the project, will build upon successful generation build-mydll-Desktop_Qt_5_12_2_MinGW_64_bit-Debug folder in the same directory where the project file, the folder has libmydll.a, mydll.dll and mydll. o three documents, we need to use the mydll.dll.

2. qtcreater create works using shared libraries as follows, engineering named UseLib.

Here Insert Picture Description

Here Insert Picture Description

Here Insert Picture Description
The mydll.h and mydll_global.h two files are copied from the project folder mydll to UseLib project source folder (F: \ QTCode \ TestCode \ TestLib \ uselib \ UseLib)
add dynamic library mainwindow.h file header file #include "mydll.h", and to define a dynamic class object library Mydll mylib, code is as follows:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "mydll.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    Mydll mylib;
private slots:
    void on_pushButton_clicked();
private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

The project mydll generated mydll.dll source files into UseLib project folder.
Add the following code in the last line of UseLib.pro file will contain a dynamic library into the current project:

LIBS +=-L$$PWD -lmydll    # $$PWD表示当前路径,mydll根据生成动态库的工程的mydll.pro里面的TARGET = mydll得到

FIG mainwindow.ui following layout:
Here Insert Picture Description
mainwindow.cpp write code as follows:

#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}
MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::on_pushButton_clicked()
{
    int a = ui->lineEdit1->text().toInt();
    int b = ui->lineEdit2->text().toInt();

    int nsum = mylib.sum(a,b);
    QString str = QString::number(nsum);
    ui->lineEditsum->setText(str);
}

Results are as follows:
Here Insert Picture Description

Second, .a static library to create and invoke
steps dynamic library is almost identical, except for the following:
1. Create a static library name is mylib, select the type "static link library", as follows: Here Insert Picture Description
2. there is still a static library sum (int a, int b) of the method, after you've created a static library, constructed under the "build-mylib-Desktop_Qt_5_12_2_MinGW_64_bit-Debug \ debug" folder will generate libmylib.a and mylib.o document, we use the is libmylib.a file.
3. Create a project using the static library, named uselib, the libmylib.a mylib.h and copied to the project under construction path and source file path
4. The last line was added in uselib.pro

LIBS +=-L$$PWD -lmylib

5. Add #include "mylib.h" in mainwindow.h and define a class object Mylib mylib;

  #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    #include <QMainWindow>
    #include "mylib.h"
    namespace Ui {
    class MainWindow;
    }
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    public:
        explicit MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    private slots:
        void on_pushButton_clicked();
        Mylib mylib;
    private:
        Ui::MainWindow *ui;
    };
    #endif // MAINWINDOW_H

6. Add the following code mainwindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}
MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::on_pushButton_clicked()
{
    int a = ui->lineEdit1->text().toInt();
    int b = ui->lineEdit2->text().toInt();
    int nsum = mylib.sum(a,b);
    QString str = QString::number(nsum);
    ui->lineEditsum->setText(str);
}

Results are as follows:

Here Insert Picture Description

Note: Creating the linux in accordance with the above procedure and call the static library pro-test success, with the system
hardware: NVIDIA Tegra X2
system: Ubuntu 16.04LTS
QT version: QT5.5.1 (Qt Creater 3.5.1)
but dynamic invocation in linux when the library will fail, I will follow specific look.

Guess you like

Origin blog.csdn.net/weixin_43935474/article/details/89043790