当前位置:网站首页>qt使用动态库(DLL)
qt使用动态库(DLL)
2022-07-30 21:15:00 【天天进步2015】
本文主要讲解在QT开发环境中如何使用VC生成的DLL及QT自身生成的DLL。至于其它情况本文不作讨论。
连接库分为2种
(1)动态连接库,通常有.h .lib .dll三个文件,功能实现在dll中
(2)静态连接库,通常有.h .lib二个文件,功能实现在lib中
由上可以看出动态库的lib和静态库的lib文件是不同的。
如果使用生成连接库的开发环境与使用连接库的开发环境相同,一般不会出什么问题,如VC写的连接库
(包括动态库和静态库)还在VC中用一般不会有什么问题的。有时候我们需要DLL跨开发环境,如以前VC下的
DLL想在QT中用。有网友说QT不支持VC生成的静态库,所以只测试QT使用动态库的情况。
【先看VC生成的DLL】
使用VC6建立Win32 dynamic-link library工程,工程中只有两个文件,编译即可生成DLL和LIB
// MFCDLL.h /
#ifndef _MFCDLL_H
#define _MFCDLL_H
#ifdef __cplusplus
extern "C" {
#endif
#ifdef DLL
// do nothing
#else
#define DLL __declspec(dllimport)
#endif
DLL void hello();
DLL int add(int a, int b);
#ifdef __cplusplus
}
#endif
#endif
// MFCDLL.cpp /
#define DLL __declspec(dllexport)
#include "MFCDLL.h"
#include <windows.h>
void hello()
{
::MessageBox(NULL, "hello world!",
"greeting", MB_OK);
}
int add(int a, int b)
{
return a + b;
}
【使用QT生成DLL】 使用QT建立动态库工程,编译即可得到DLL(无LIB文件)
// qtdll_global.h //
#ifndef QTDLL_GLOBAL_H
#define QTDLL_GLOBAL_H
#include
#if defined(QTDLL_LIBRARY)
# define QTDLLSHARED_EXPORT Q_DECL_EXPORT
#else
# define QTDLLSHARED_EXPORT Q_DECL_IMPORT
#endif
#endif // QTDLL_GLOBAL_H
// qtdll.h /
#ifndef QTDLL_H
#define QTDLL_H
#include "qtdll_global.h"
class QTDLLSHARED_EXPORT QTDLL
{
public:
QTDLL();
public:
int add(int a, int b);
};
#endif // QTDLL_H
// qtdll.cpp ///
#include "qtdll.h"
QTDLL::QTDLL()
{
}
int QTDLL::add(int a, int b)
{
return a + b;
}
【QT显式加载VC生成的DLL】
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QLibrary>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// DLL显式加载,只需要DLL文件即可,不需要.H和.LIB文件
// 需要将DLL放到可执行目录中
typedef void(*FUN1)();
typedef int(*FUN2)(int, int);
QLibrary lib("MFCDLL.dll");
if (lib.load()) {
qDebug() << "load ok!";
FUN1 hello = (FUN1)lib.resolve("hello");
FUN2 add = (FUN2)lib.resolve("add");
if (hello) {
qDebug() << "load hello ok!";
hello();
}
if (add) {
qDebug() << "load add ok!";
qDebug() << add(3, 5);
}
} else {
qDebug() << "load error!";
}
}
【QT隐式加载VC生成的DLL】
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "lib/MFCDLL.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// DLL隐式加载,只需要.DLL .H和.LIB文件
// 1需要将DLL放到可执行目录中
// 2将LIB路径设置到项目PRO文件中
// 3将头文件包含进来,如果不包含需要自已声明函数原型及来源(本质与包含头文件相同)
hello();
qDebug() << add(5, 6);
qDebug() << "ok";
}
pro工程文件中要设置LIB文件路径
# lib文件路径
LIBS += "F:/lib/MFC_DLL_TEST_WITH_QT_2/lib/MFCDLL.lib"
【QT使用QT生成的动态库,隐式】
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include "lib/qtdll.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// QT使用QT生成的DLL
// 1. 包含头文件
// 2. 在工程文件中指定lib路径
// 3. 将动态库拷贝到可执行文件目录
QTDLL dll;
qDebug() << dll.add(3, 5);
}
pro工程文件中的设置
LIBS += "F:/lib/QT_DLL_TEST_WITH_DLL/lib/QTDLL.dll"
边栏推荐
- Swift RegexBuilder Vs. Raku Grammar
- Typescript 严格模式有多严格?
- [Nuxt 3] (十三) Nuxt 是如何工作的?
- 一个网络两种用途!南开&哈工程提出TINet,通过细化纹理和边缘,在显著性目标检测和伪装目标检测上实现双SOTA!...
- 微信公众号授权登录后报redirect_uri参数错误的问题
- WeChat reading, export notes
- LeetCode·每日一题·952.按公因数计算最大组件大小·并查集
- 【Nacos】解决Nacos下载速度缓慢的问题
- Why do so many people who teach themselves software testing give up later...
- 【机器学习】梯度下降背后的数学之美
猜你喜欢
MySQL60题作业
系统结构考点之CRAY-1向量处理机
Niu Ke Xiaobaiyue Race 53 A-E
8 ways to get element attributes in JS
2022-07-29 mysql/stonedb慢SQL-Q17-分析
[Limited Time Bonus] 21-Day Learning Challenge - MySQL from entry to mastery
kubernetes
Deep Kalman Filter Network for Video Compression Artifact Removal
微信公众号授权登录后报redirect_uri参数错误的问题
【深度学习】对迁移学习中域适应的理解和3种技术的介绍
随机推荐
ELF:加载过程
我是如何让公司后台管理系统焕然一新的(上) -性能优化
系统结构考点之CRAY-1向量处理机
拿什么来保护数据安全?基层数据安全体系建设待提升
你需要知道的ES6—ES13开发技巧
对一次生产环境产生OOM的记录,结论:除非在自己完全有把握的情况下,否则不要偷懒查询无用字段
类和对象——上
The structure of knowledge in the corners of the C language
Knowledge of C language corners of byte alignment
【Nacos】解决Nacos下载速度缓慢的问题
数字货币期货现货交易技巧,把握关键进场的买入点!(纯干货)
JSESSIONID description in cookie
ML.NET相关资源整理
Google Earth Engine ——
Motion Tuned Spatio-temporal Quality Assessmentof Natural Videos
深入浅出富文本编辑器
外包干了三年,废了...
Simple configuration of three-tier architecture
MySQL笔记2(函数,约束,多表查询,事务)
Mysql8创建用户以及赋权操作