qt使用动态库(DLL)

本文主要讲解在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
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. // MFCDLL.h  /  
  2. #ifndef _MFCDLL_H  
  3. #define _MFCDLL_H  
  4.   
  5. #ifdef __cplusplus  
  6. extern "C" {  
  7. #endif  
  8.   
  9. #ifdef DLL  
  10.     // do nothing  
  11. #else  
  12. #define DLL __declspec(dllimport)  
  13. #endif  
  14.   
  15. DLL void hello();  
  16. DLL int add(int a, int b);  
  17.   
  18. #ifdef __cplusplus  
  19. }  
  20. #endif  
  21.   
  22. #endif  
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. // MFCDLL.cpp  /  
  2. #define DLL __declspec(dllexport)  
  3. #include "MFCDLL.h"  
  4. #include <windows.h>  
  5.   
  6. void hello()  
  7. {  
  8. ::MessageBox(NULL, "hello world!",   
  9. "greeting", MB_OK);  
  10. }  
  11.   
  12. int add(int a, int b)  
  13. {  
  14. return a + b;  
  15. }  
【使用QT生成DLL】使用QT建立动态库工程,编译即可得到DLL(无LIB文件)
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. // qtdll_global.h  //  
  2. #ifndef QTDLL_GLOBAL_H  
  3. #define QTDLL_GLOBAL_H  
  4.   
  5. #include  
  6.   
  7. #if defined(QTDLL_LIBRARY)  
  8. #  define QTDLLSHARED_EXPORT Q_DECL_EXPORT  
  9. #else  
  10. #  define QTDLLSHARED_EXPORT Q_DECL_IMPORT  
  11. #endif  
  12.   
  13. #endif // QTDLL_GLOBAL_H  
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. // qtdll.h  /  
  2. #ifndef QTDLL_H  
  3. #define QTDLL_H  
  4.   
  5. #include "qtdll_global.h"  
  6.   
  7. class QTDLLSHARED_EXPORT QTDLL  
  8. {  
  9. public:  
  10.     QTDLL();  
  11.   
  12. public:  
  13.     int add(int a, int b);  
  14. };  
  15.   
  16. #endif // QTDLL_H  
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. // qtdll.cpp  ///  
  2. #include "qtdll.h"  
  3.   
  4.   
  5. QTDLL::QTDLL()  
  6. {  
  7. }  
  8.   
  9. int QTDLL::add(int a, int b)  
  10. {  
  11.     return a + b;  
  12. }  
【QT显式加载VC生成的DLL】
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include "mainwindow.h"  
  2. #include "ui_mainwindow.h"  
  3. #include <QLibrary>  
  4. #include <QDebug>  
  5.   
  6. MainWindow::MainWindow(QWidget *parent) :  
  7.     QMainWindow(parent),  
  8.     ui(new Ui::MainWindow)  
  9. {  
  10.     ui->setupUi(this);  
  11.   
  12.     // DLL显式加载,只需要DLL文件即可,不需要.H和.LIB文件  
  13.     // 需要将DLL放到可执行目录中  
  14.     typedef void(*FUN1)();  
  15.     typedef int(*FUN2)(intint);  
  16.   
  17.     QLibrary lib("MFCDLL.dll");  
  18.     if (lib.load()) {  
  19.         qDebug() << "load ok!";  
  20.   
  21.         FUN1 hello = (FUN1)lib.resolve("hello");  
  22.         FUN2 add = (FUN2)lib.resolve("add");  
  23.         if (hello) {  
  24.             qDebug() << "load hello ok!";  
  25.             hello();  
  26.         }  
  27.         if (add) {  
  28.             qDebug() << "load add ok!";  
  29.             qDebug() << add(3, 5);  
  30.         }  
  31.     } else {  
  32.         qDebug() << "load error!";  
  33.     }  
  34. }  
【QT隐式加载VC生成的DLL】
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include "mainwindow.h"  
  2. #include "ui_mainwindow.h"  
  3. #include "lib/MFCDLL.h"  
  4. #include <QDebug>  
  5.   
  6. MainWindow::MainWindow(QWidget *parent) :  
  7.     QMainWindow(parent),  
  8.     ui(new Ui::MainWindow)  
  9. {  
  10.     ui->setupUi(this);  
  11.   
  12.     // DLL隐式加载,只需要.DLL .H和.LIB文件  
  13.     // 1需要将DLL放到可执行目录中  
  14.     // 2将LIB路径设置到项目PRO文件中  
  15.     // 3将头文件包含进来,如果不包含需要自已声明函数原型及来源(本质与包含头文件相同)  
  16.     hello();  
  17.     qDebug() << add(5, 6);  
  18.     qDebug() << "ok";  
  19. }  
pro工程文件中要设置LIB文件路径
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. # lib文件路径  
  2. LIBS += "F:/lib/MFC_DLL_TEST_WITH_QT_2/lib/MFCDLL.lib"  
【QT使用QT生成的动态库,隐式】
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include "mainwindow.h"  
  2. #include "ui_mainwindow.h"  
  3. #include <QDebug>  
  4. #include "lib/qtdll.h"  
  5.   
  6. MainWindow::MainWindow(QWidget *parent) :  
  7.     QMainWindow(parent),  
  8.     ui(new Ui::MainWindow)  
  9. {  
  10.     ui->setupUi(this);  
  11.   
  12.     // QT使用QT生成的DLL  
  13.     // 1. 包含头文件  
  14.     // 2. 在工程文件中指定lib路径  
  15.     // 3. 将动态库拷贝到可执行文件目录  
  16.     QTDLL dll;  
  17.     qDebug() << dll.add(3, 5);  
  18. }  
pro工程文件中的设置
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. LIBS += "F:/lib/QT_DLL_TEST_WITH_DLL/lib/QTDLL.dll"  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值