20、Qt使用QLibrary类在运行时动态加载外部库

一、功能说明

创建一个项目,里面包含两个子项目,

子项目1生成动态链接库dll,里面包含一个加法和一个减法;

子项目2是带界面的可执行文件exe,使用QLibrary类来调用子项目1的dll。

注:QLibrary类在运行时动态加载外部库(只需要动态链接库.dll)

二、创建子项目1

新建项目,选择“其他项目”、“子目录项目”

更改总项目名称和位置

选择编译器

默认,点击"完成&添加子项目"

选择子项目1模板,“Library”、“C++库”

更改类型和名称

默认

默认

默认

完成后,项目文件结构如下

右击“LibraryDll”,添加新文件

选择C++、C++ Class

更改名称

默认

在LibraryDll.pro中添加代码,更改子项目1的dll生成位置

#-------------------------------------------------
#
# Project created by QtCreator 2023-09-27T14:10:33
#
#-------------------------------------------------

QT -= gui

TARGET = LibraryDll
TEMPLATE = lib

DEFINES += LIBRARYDLL_LIBRARY

CONFIG(debug , debug | release) {
win32:!wince{
DESTDIR = $$PWD/../dll
}
unix {
DESTDIR = $$PWD/../dll
}
} else {
win32:!wince{
DESTDIR = $$PWD/../dll
}
unix {
DESTDIR = $$PWD/../dll
}
}

# 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 += \
librarydll.cpp \
dllmain.cpp

HEADERS += \
librarydll.h \
librarydll_global.h \
dllmain.h

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

更改librarydll.h代码如下

#ifndef LIBRARYDLL_H
#define LIBRARYDLL_H

#include "librarydll_global.h"

class LIBRARYDLLSHARED_EXPORT LibraryDll
{

public:
    LibraryDll();

    int addFun(int , int);
    int subFun(int , int);
};

#endif // LIBRARYDLL_H

更改librarydll.cpp代码如下

#include "librarydll.h"

LibraryDll::LibraryDll()
{
}

int LibraryDll::addFun(int value1, int value2)
{
    return (value1 + value2);
}

int LibraryDll::subFun(int value1, int value2)
{
    return (value1 - value2);
}

更改dllmain.h代码如下

#ifndef DLLMAIN_H
#define DLLMAIN_H

#include "librarydll_global.h"

extern "C" LIBRARYDLLSHARED_EXPORT int addFun(int , int);
extern "C" LIBRARYDLLSHARED_EXPORT int subFun(int , int);

#endif // DLLMAIN_H

更改dllmain.cpp代码如下

#include "dllmain.h"
#include "librarydll.h"

static LibraryDll LibraryInstance;

int addFun(int value1, int value2)
{
    return LibraryInstance.addFun(value1, value2);
}

int subFun(int value1, int value2)
{
    return LibraryInstance.subFun(value1, value2);
}

右击“LibraryDll”,重新构建

构建完成后,在dll目录下生成dll

三、创建子项目2

右击“MyLibrary”,新子项目

选择“Application”、Qt Widgets Application

更改子项目2名称

默认

默认

默认

在mainwindow.ui中添加如下控件

 更改mainwindow.h代码

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

//定义函数指针
typedef int (*addFun)(int , int);
typedef int (*subFun)(int , int);

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

更改mainwindow.cpp代码

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QLibrary>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    QString path = QApplication::applicationDirPath() + "/LibraryDll.dll";

    QLibrary dllLib;
    dllLib.setFileName(path);
    if(dllLib.load())
    {
        addFun myFun = reinterpret_cast<addFun>(dllLib.resolve("addFun"));
        int ret = myFun(ui->lineEdit->text().toInt(), ui->lineEdit_2->text().toInt());
        ui->label->setText(QString::number(ret));

        dllLib.unload();
    }
    else
    {
        ui->label->setText("load dll error: " + dllLib.errorString());
    }
}

void MainWindow::on_pushButton_2_clicked()
{
    QString path = QApplication::applicationDirPath() + "/LibraryDll.dll";

    QLibrary dllLib;
    dllLib.setFileName(path);
    if(dllLib.load())
    {
        subFun myFun = reinterpret_cast<subFun>(dllLib.resolve("subFun"));
        int ret = myFun(ui->lineEdit->text().toInt(), ui->lineEdit_2->text().toInt());
        ui->label->setText(QString::number(ret));

        dllLib.unload();
    }
    else
    {
        ui->label->setText(dllLib.errorString());
    }
}

四、运行测试

输入5和3,点击加法

再点击减法

  • 20
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值