一、功能说明
创建一个项目,里面包含两个子项目,
子项目1生成动态链接库dll,里面包含一个加法和一个减法;
子项目2是带界面的可执行文件exe,使用QLibrary类来调用子项目1的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,点击加法
再点击减法