当前位置: 首页 > news >正文

Qt生成动态链接库并使用动态链接库

项目结构

整个工程由一个主程序构成和一个模块构成(dll)。整个工程的结构目录如下

   Define.priMyProject.proMyProject.pro.user
+---bin
+---MainProgrammain.cppMainProgram.proMainProgram.pro.userwidget.cppwidget.hwidget.ui
+---MathDllMathDll.proMathDll.pro.userMyMath.cppMyMath.h

qmake文件介绍

1 MyProject.pro

TEMPLATE = subdirs #多工程项目
CONFIG += ordered #指定编译顺序
SUBDIRS = MathDll \ #模块(dll)MainProgram #主工程

2 Define.pri

win32 {CONFIG(release, debug|release){contains(QT_ARCH, i386) {BIN_PATH=release_x86_} else {BIN_PATH=release_x64_}} else {contains(QT_ARCH, i386) {BIN_PATH=debug_x86_} else {BIN_PATH=debug_x64_}}
}DESTDIR = $$PWD/bin/$$BIN_PATH #生成的二进制文件(可执行文件或者库)的目标位置,放在pri文件中,在有pro文件包含这样就不用重复写

3 MathDll.pro

QT -= guiTEMPLATE = libCONFIG += c++11# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked 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
DEFINES += MATHDLL_LIBRARY #使用预定义来控制是导出函数符号到dll还是导出dll中的函数符号# You can also make your code fail to compile if it uses 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.0include($$PWD/../Define.pri) #引入pri文件SOURCES += \MyMath.cpp \MyMath.h
#拷贝头文件到指定目录,以便其它程序能够使用该dll
QMAKE_POST_LINK += xcopy /y/F \"$$PWD/MyMath.h\" \"$$PWD/../bin/includePath\\\" $$escape_expand(\\n\\t)# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

4 MainProgram.pro

#-------------------------------------------------
#
# Project created by QtCreator 2023-12-09T17:29:33
#
#-------------------------------------------------QT       += core guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsTARGET = MainProgram
TEMPLATE = app# 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.0include($$PWD/../Define.pri) #引入pri文件
LIBS += -L$$PWD/../bin/$$BIN_PATH #设置搜索链接动态库的目录CONFIG += c++11INCLUDEPATH += $$PWD/../bin/includePath #设置搜索头文件目录
LIBS += -lMathDll #链接库文件SOURCES += \main.cpp \widget.cppHEADERS += \widget.hFORMS += \widget.ui# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

注意 :

LIBS += -L$$PWD/../bin/$$BIN_PATH

这句话是很重要的,在程序编译链接时会使用到动态库的 lib 文件,用于生成程序。而默认链接的位置是 MainProgram 目录下,如果没有该语句就会提示找不到 MathDll.lib 文件,可执行程序生成失败。
在这里插入图片描述

源文件分析

1 MyMath.h

#ifndef MYMATH_H_
#define MYMATH_H_
//包含QObject才能识别 Q_DECL_EXPORT Q_DECL_IMPORT
#include <QObject>
//预定义来控制导入导出符号
#if defined (MATHDLL_LIBRARY)
#define MATHDLL_LIBRRY_EXPORT Q_DECL_EXPORT
#else
#define MATHDLL_LIBRRY_EXPORT Q_DECL_IMPORT
#endifclass MATHDLL_LIBRRY_EXPORT MyMath{
public:MyMath();~MyMath();int add(int a, int b);
};#endif

2 widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include "MyMath.h"Widget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget)
{ui->setupUi(this);
}Widget::~Widget()
{delete ui;
}void Widget::on_pushButton_clicked()
{int a = ui->lineEdit->text().toInt();int b = ui->lineEdit_2->text().toInt();MyMath myMath;int sum = myMath.add(a, b); //使用动态链接库中的函数ui->label->setText(ui->label->text() + QString::number(sum));
}

效果

在这里插入图片描述

码云地址

https://gitee.com/wenbai123/my-project
http://www.lryc.cn/news/257563.html

相关文章:

  • E4990A 阻抗分析仪,20 Hz 至 10/20/30/50/120 MHz
  • k8s volumes and data
  • 万宾科技智能水环境综合治理监测系统效果
  • 掌控安全 暖冬杯 CTF Writeup By AheadSec
  • jQuery-操作DOM
  • 高级网工在Linux服务器抓包,少不了这几条常用的tcpdump命令。
  • Hough算法数学原理
  • 基于Debain安装 Docker 和 Docker Compose
  • gittee使用教学
  • q2-qt-多线程
  • 指针,函数指针,二级指针,指针传参,回调函数,指针步长
  • StringUtils.isEmpty()方法过期的替代方法
  • 智慧电力运维综合辅助监控系统
  • v-model和:model的区别
  • 网络攻击(二)--情报搜集阶段
  • oracle异常:ORA-03297:文件包含在请求的 RESIZE 值以外使用的数据
  • Redis 环境搭建
  • 什么是Helpdesk?对工程师有什么帮助?
  • flutter添加全局水印
  • Usergolang 一些优质关于sip协议包
  • MYSQL数据类型详解
  • 解决vue3 动态引入报错问题
  • Mysql dumpling 导入导出sql文件
  • 【数字经济】你必须知道的SABOE数字化转型
  • 【Python网络爬虫入门教程2】成为“Spider Man”的第二课:观察目标网站、代码编写
  • vue2和vue3中注意全局属性的区别(例如全局使用axios )
  • 数字系统设计(EDA)实验报告【出租车计价器】
  • 309. 买卖股票的最佳时机含冷冻期(leetcode) 动态规划思想
  • 3D渲染和动画制作软件KeyShot Pro mac附加功能
  • 集合的几个遍历方法