QT Dll使用

it2026-08-12  9

文章目录

测试思路相同工具下dll测试实现dll实现测试程序实现纯接口dll 不同工具下dll测试用vs2015编写dll测试 支持相同功能的两个不同dll创建dll测试程序

测试思路

用qt mingw工具生成dll,用qt测试端测试用VS2015生成dll,用qt测试端测试加载相同dll的不同版本(场景:libusb分win32版本和通用版本,如何一套代码适配两种版本) 注:测试环境 win10系统

相同工具下dll测试

实现dll

1)新建工程: 项目 --> Library --> C++库, 类型选择共享库 2)文件信息如下

libusbwin32.pro

QT -= gui TARGET = libusbwin32 TEMPLATE = lib DEFINES += LIBUSBWIN32_LIBRARY # 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 += \ libusbwin32.cpp HEADERS += \ libusbwin32.h \ libusbwin32_global.h unix { target.path = /usr/lib INSTALLS += target }

libusbwin32_global.h

#ifndef LIBUSBWIN32_GLOBAL_H #define LIBUSBWIN32_GLOBAL_H #include <QtCore/qglobal.h> #if defined(LIBUSBWIN32_LIBRARY) # define LIBUSBWIN32SHARED_EXPORT Q_DECL_EXPORT #else # define LIBUSBWIN32SHARED_EXPORT Q_DECL_IMPORT #endif #endif // LIBUSBWIN32_GLOBAL_H

libusbwin32.h

#ifndef LIBUSBWIN32_H #define LIBUSBWIN32_H #include "libusbwin32_global.h" class LIBUSBWIN32SHARED_EXPORT Libusbwin32 { public: Libusbwin32(); void dump_win32(); }; #endif // LIBUSBWIN32_H

libusbwin32.cpp

#include "libusbwin32.h" #include <QDebug> Libusbwin32::Libusbwin32() { } void Libusbwin32::dump_win32() { qDebug()<<"libusb for win32"; }

编译后生成文件如下: libusbwin32.a, libusbwin32.dll, libusbwin32.o

实现测试程序

工程为multilibusb,创建工程,将dll的头文件libusbwin32.h和libusbwin32_global.h放到工程目录下 在工程源目录下创建lib子目录,将dll复制到该目录下 注:在执行程序时,将dll复制到exe目录下 multilibusb.pro

QT -= gui CONFIG += c++11 console CONFIG -= app_bundle DEFINES += QT_DEPRECATED_WARNINGS SOURCES += \ main.cpp LIBS += -L$$PWD/lib LIBS += -llibusbwin32 # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target HEADERS += \ libusbwin32.h

main.cpp

#include "libusbwin32.h" #include <QCoreApplication> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); Libusbwin32 libusb; libusb.dump_win32(); return a.exec(); }

实现纯接口dll

libusb对外开放的是一系列函数,所以,模仿实现。 libusbwin32.pro

QT -= gui TARGET = libusbwin32 TEMPLATE = lib DEFINES += LIBUSBWIN32_LIBRARY DEFINES += QT_DEPRECATED_WARNINGS SOURCES += \ libusbwin32.cpp HEADERS += \ libusbwin32.h unix { target.path = /usr/lib INSTALLS += target }

libusbwin32.h

#ifndef LIBUSBWIN32_H #define LIBUSBWIN32_H #include <QtCore/qglobal.h> #if defined(LIBUSBWIN32_LIBRARY) # define LIBUSBWIN32SHARED_EXPORT Q_DECL_EXPORT #else # define LIBUSBWIN32SHARED_EXPORT Q_DECL_IMPORT #endif extern "C" { LIBUSBWIN32V2SHARED_EXPORT void dump_win32(); } #endif // LIBUSBWIN32_H

libusbwin32.cpp

#include "libusbwin32.h" #include <QDebug> void dump_win32() { qDebug()<<"libusb for win32"; }

不同工具下dll测试

用vs2015编写dll

请参考 C/C++ Windows编程—调用DLL程序的2种方法

测试

1)头文件放入测试工程目录下,引用头文件 2)dll放牧测试工程lib目录下 3)dll放于生成的exe目录下 测试通过

支持相同功能的两个不同dll

创建dll

1)libusbwin32v2 2)libusb

测试程序

在工程文件中通过 DEFINES += LIBUSBWIN32 添加宏开关在工程文件中通过 contains(DEFINES, LIBUSBWIN32) 引入响应的dll文件源文件中通过 LIBUSBWIN32 开启不同的源文件 目录结构如下 multilibusb ├── lib │   ├── libusb.dll │   └── libusbwin32v2.dll ├── libusb.h ├── libusb_common.h ├── libusbwin32v2.h ├── main.cpp ├── multilibusb.pro └── multilibusb.pro.user

multilibusb.pro

QT -= gui CONFIG += c++11 console CONFIG -= app_bundle DEFINES += LIBUSBWIN32 DEFINES += QT_DEPRECATED_WARNINGS SOURCES += \ main.cpp LIBS += -L$$PWD/lib contains(DEFINES, LIBUSBWIN32){ LIBS += -llibusbwin32v2 } else{ LIBS += -llibusb } # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target HEADERS += \ libusb.h

libusb_common.h

#ifndef LIBUSB_H #define LIBUSB_H #ifdef LIBUSBWIN32 #include "libusbwin32v2.h" #define dump dump_win32 #else #include "libusb.h" #endif #endif // LIBUSB_H

libusbwin32v2.h

#ifndef LIBUSBWIN32V2_H #define LIBUSBWIN32V2_H #include <QtCore/qglobal.h> #if defined(LIBUSBWIN32V2_LIBRARY) # define LIBUSBWIN32V2SHARED_EXPORT Q_DECL_EXPORT #else # define LIBUSBWIN32V2SHARED_EXPORT Q_DECL_IMPORT #endif extern "C" { LIBUSBWIN32V2SHARED_EXPORT void dump_win32(); } #endif // LIBUSBWIN32V2_H

libusb.h

#ifndef LIBUSB_H #define LIBUSB_H #include <QtCore/qglobal.h> #if defined(LIBUSB_LIBRARY) # define LIBSHARED_EXPORT Q_DECL_EXPORT #else # define LIBSHARED_EXPORT Q_DECL_IMPORT #endif extern "C" { LIBSHARED_EXPORT void dump(); } #endif // LIBUSB_H

main.cpp

#include "libusb_common.h" #include <QCoreApplication> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); dump(); return a.exec(); }
最新回复(0)