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_Hlibusbwin32.h
#ifndef LIBUSBWIN32_H #define LIBUSBWIN32_H #include "libusbwin32_global.h" class LIBUSBWIN32SHARED_EXPORT Libusbwin32 { public: Libusbwin32(); void dump_win32(); }; #endif // LIBUSBWIN32_Hlibusbwin32.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.hmain.cpp
#include "libusbwin32.h" #include <QCoreApplication> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); Libusbwin32 libusb; libusb.dump_win32(); return a.exec(); }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_Hlibusbwin32.cpp
#include "libusbwin32.h" #include <QDebug> void dump_win32() { qDebug()<<"libusb for win32"; }请参考 C/C++ Windows编程—调用DLL程序的2种方法
1)头文件放入测试工程目录下,引用头文件 2)dll放牧测试工程lib目录下 3)dll放于生成的exe目录下 测试通过
1)libusbwin32v2 2)libusb
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.hlibusb_common.h
#ifndef LIBUSB_H #define LIBUSB_H #ifdef LIBUSBWIN32 #include "libusbwin32v2.h" #define dump dump_win32 #else #include "libusb.h" #endif #endif // LIBUSB_Hlibusbwin32v2.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_Hlibusb.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_Hmain.cpp
#include "libusb_common.h" #include <QCoreApplication> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); dump(); return a.exec(); }