当前位置:网站首页>QT 动态dll的创建和调用
QT 动态dll的创建和调用
2022-07-22 02:24:00 【God港】
一、DLL的生成
1.工程文件创建
命名后直接下一步即可。
2.类文件创建
本例子我们创建2个类进行测试
工程结构如下:
CommonToolDll代码:
#ifndef COMMONTOOLDLL_H
#define COMMONTOOLDLL_H
#include "commontooldll_global.h"
class COMMONTOOLDLLSHARED_EXPORT CommonToolDll
{
public:
CommonToolDll();
static double Sum(double a,double b);
};
#endif // COMMONTOOLDLL_H
#include "commontooldll.h"
CommonToolDll::CommonToolDll()
{
}
double CommonToolDll::Sum(double a, double b)
{
return a+b;
}
PrintString代码:
#ifndef PRINTSTRING_H
#define PRINTSTRING_H
#include<QString>
#include "commontooldll_global.h"
class COMMONTOOLDLLSHARED_EXPORT PrintString
{
public:
PrintString();
static QString PrintHexString(QByteArray str);
};
#endif // PRINTSTRING_H
#include "printstring.h"
PrintString::PrintString()
{
}
QString PrintString::PrintHexString(QByteArray str)
{
return str.toHex();
}
注意事项:每一个新建的类都得用宏 COMMONTOOLDLLSHARED_EXPORT进行修饰,不然我们后面无法调用此类,这个宏是定义在全局头文件中的。
3.构建
右键项目工程,运行构建即可。
4.效果
我们需要的dll和lib已经生成,本例子只使用了MSVC编译器,如果使用MinGW会同时生成.a的库文件
二、调用
1.新建一个Widget项目
2.环境配置
将开始的写的头文件放入我们的including文件夹中
在pro中添加头文件路径:
添加lib文件夹翻入dll
导入lib
右键项目,选择添加库-》外部库
然后看见pro文件中的LIBS已经添加,当然也可以手动写入
界面测试代码:
#include "widget.h"
#include "ui_widget.h"
#include"commontooldll.h"
#include"printstring.h"
#include<QDebug>
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().toDouble();
int b=ui->lineEdit_2->text().toDouble();
ui->line_sum->setText(QString::number(a+b));
QByteArray aa("FF");
ui->textEdit->setText(PrintString::PrintHexString(aa));
}
运行效果:
总结:
1.每一个类都要添加外部库宏声明,不然无法正常调用
2.注意调用时配置头文件和库文件
3.将dll放到工程文件目录下
边栏推荐
- 对原数组有影响的几个方法
- The installation and use of harbor+trivy -- the way to build a dream
- Halcon displays point clouds in TXT file format
- codeforces每日5题(均1500)-第二十二天
- Tdengine learning notes
- Redis' expansion plan
- How to optimize this SQL?
- harbor+trivy的安装使用——筑梦之路
- 码蹄集 - MT3182 - 填矩阵
- The difference between static variables and global variables
猜你喜欢
随机推荐
【C语言-文件】数据终于可以出内存,到外面的世界看看了/(ㄒoㄒ)/~~
Wechat applet Decompilation
ClickHouse相关SQL总结:创建表、分区/主键/排序设置、删除表、删除分区、修改表字段
Distributed link tracking skywalking practice
Event handling of lvgl
[C language - program compilation] how does the line by line code get to an executable program step by step?
[C language - file] the data can finally be out of the memory. Go to the outside world to have a look / (o)/~~
Tdengine experimental cluster establishment success
加快标准体系建设,促进数据安全产业高质量发展
RuntimeWarning: Glyph 25152 missing from current font. font.set_text(s, 0.0, flags=flags)
Introduction to microservices
时序数据库
Part 16: ADC, CAD and DMA direct memory access in stm32
Tdengine learning notes
FPGA image processing learning face recognition
旋转矩阵_百度百科
df. drop_ Duplicates() explanation + usage
商业智能BI分析思维:生产制造行业的现金周期(二)
卷妹的面试小抄每日更新Day1
Golang language cli Library