为什么在构建使用静态库的项目时出现错误?我的静态库正在使用libusb-1.0

问题描述

请帮助我。我无法在DeviceTest项目中使用静态库。下面是我的静态libDevice。在其中,我使用lusb-1.0。 My libDevice building is successful

# =============================== Device.pro ===============================
QT -= gui

TEMPLATE = lib
CONfig += staticlib

# ...

LIBS += -lusb-1.0

SOURCES += \
    Device.cpp

HEADERS += \
    Device.h

# =============================== Device.h =============================== 
#pragma once

#include <QObject>
#include <QDebug>
#include <QString>
#include <QByteArray>

#include <libusb-1.0/libusb.h>

class Device : public QObject
{
    Q_OBJECT

    libusb_device_handle* handle;  /* handle for USB device */
public:
    explicit Device(QObject* parent = nullptr);

    void printText(const QString &text);
    void initDevice();
    void transferBytes(const QByteArray &arr);
    void releaseDevice();
};

# =============================== Device.cpp ===============================
#include "Device.h"


Device::Device(QObject *parent) : QObject(parent)
{

}

void Device::printText(const QString &text)
{
    qDebug() << "print text - " << text << Qt::endl;
}

void Device::initDevice()
{
    int error = libusb_init(nullptr);
    qDebug() << "Init device" << error << Qt::endl;

    auto handle = libusb_open_device_with_vid_pid(nullptr,0xdd4,0x1a8);
    error = libusb_claim_interface(handle,0);
}

void Device::transferBytes(const QByteArray &arr)
{
    int numBytes = 0;
    auto error = libusb_bulk_transfer(handle,0x02,(unsigned char *)arr.data(),arr.size(),&numBytes,1000);
    qDebug() << "transferBytes" << arr << "error" << libusb_error_name(error) << Qt::endl;
}

void Device::releaseDevice()
{
    auto error = libusb_release_interface(handle,0);
    libusb_close(handle);
    libusb_exit(nullptr);
    handle = nullptr;
    qDebug() << "releaseDevice" << "error" << libusb_error_name(error) << Qt::endl;
}

新项目(DeviceTest):

# =============================== DeviceTest.pro ===============================
QT -= gui
CONfig += c++11 console
CONfig -= app_bundle
# ...
SOURCES += \
        main.cpp

HEADERS += \
    DeviceLib/Device.h

LIBS += -lusb-1.0

unix:!macx: LIBS += -L$$PWD/DeviceLib/ -lDevice
INCLUDEPATH += $$PWD/DeviceLib
DEPENdpath += $$PWD/DeviceLib
unix:!macx: PRE_TARGETDEPS += $$PWD/DeviceLib/libDevice.a

# =============================== main.cpp ===============================
#include <QCoreApplication>

#include "DeviceLib/Device.h"


int main(int argc,char *argv[])
{
    QCoreApplication a(argc,argv);

    Device device;
    device.printText("Hello World!");

    return a.exec();
}

构建DeviceTest项目不成功。构建它时出现错误

Build output:
g++ -c -pipe -g -std=gnu++11 -Wall -Wextra -D_REENTRANT -fPIC -DQT_DEPRECATED_WARNINGS -DQT_QML_DEBUG -DQT_CORE_LIB -I../DeviceLibTest -I. -I../DeviceLibTest/DeviceLib -I/home/jp/Qt/5.15.0/gcc_64/include -I/home/jp/Qt/5.15.0/gcc_64/include/QtCore -I. -I/home/jp/Qt/5.15.0/gcc_64/mkspecs/linux-g++ -o main.o ../DeviceLibTest/main.cpp
g++ -pipe -g -std=gnu++11 -Wall -Wextra -dM -E -o moc_predefs.h /home/jp/Qt/5.15.0/gcc_64/mkspecs/features/data/dummy.cpp
/home/jp/Qt/5.15.0/gcc_64/bin/moc -DQT_DEPRECATED_WARNINGS -DQT_QML_DEBUG -DQT_CORE_LIB --include /home/user/FolderMain/Projects_Test/DeviceLibTest/build-DeviceLibTest-jpDesktop_Qt_5_15_0_GCC_64bit-Debug/moc_predefs.h -I/home/jp/Qt/5.15.0/gcc_64/mkspecs/linux-g++ -I/home/user/FolderMain/Projects_Test/DeviceLibTest/DeviceLibTest -I/home/user/FolderMain/Projects_Test/DeviceLibTest/DeviceLibTest/DeviceLib -I/home/jp/Qt/5.15.0/gcc_64/include -I/home/jp/Qt/5.15.0/gcc_64/include/QtCore -I. -I/usr/include/c++/9 -I/usr/include/x86_64-linux-gnu/c++/9 -I/usr/include/c++/9/backward -I/usr/lib/gcc/x86_64-linux-gnu/9/include -I/usr/local/include -I/usr/include/x86_64-linux-gnu -I/usr/include ../DeviceLibTest/DeviceLib/Device.h -o moc_Device.cpp
g++ -c -pipe -g -std=gnu++11 -Wall -Wextra -D_REENTRANT -fPIC -DQT_DEPRECATED_WARNINGS -DQT_QML_DEBUG -DQT_CORE_LIB -I../DeviceLibTest -I. -I../DeviceLibTest/DeviceLib -I/home/jp/Qt/5.15.0/gcc_64/include -I/home/jp/Qt/5.15.0/gcc_64/include/QtCore -I. -I/home/jp/Qt/5.15.0/gcc_64/mkspecs/linux-g++ -o moc_Device.o moc_Device.cpp
g++ -Wl,-rpath,/home/jp/Qt/5.15.0/gcc_64/lib -o DeviceLibTest main.o moc_Device.o   -lusb-1.0 -L/home/user/FolderMain/Projects_Test/DeviceLibTest/DeviceLibTest/DeviceLib/ -lDevice /home/jp/Qt/5.15.0/gcc_64/lib/libQt5Core.so -lpthread   

这些是我得到的错误。我使用Qt Creator对话框窗口添加了我的库(上下文菜单->添加库...) 错误undefined reference to 'libusb_init',undefined reference to 'libusb_open_device_with_vid_pid',undefined reference to 'libusb_claim_interface' ...

errors:
/usr/bin/ld: /home/user/FolderMain/Projects_Test/DeviceLibTest/DeviceLibTest/DeviceLib//libDevice.a(Device.o): in function `Device::initDevice()':
/home/user/FolderMain/Projects_Test/DeviceLib/build-Device-jpDesktop_Qt_5_15_0_GCC_64bit-Debug/../Device/Device.cpp:15: undefined reference to `libusb_init'
/usr/bin/ld: /home/user/FolderMain/Projects_Test/DeviceLib/build-Device-jpDesktop_Qt_5_15_0_GCC_64bit-Debug/../Device/Device.cpp:18: undefined reference to `libusb_open_device_with_vid_pid'
/usr/bin/ld: /home/user/FolderMain/Projects_Test/DeviceLib/build-Device-jpDesktop_Qt_5_15_0_GCC_64bit-Debug/../Device/Device.cpp:19: undefined reference to `libusb_claim_interface'
/usr/bin/ld: /home/user/FolderMain/Projects_Test/DeviceLibTest/DeviceLibTest/DeviceLib//libDevice.a(Device.o): in function `Device::transferBytes(QByteArray const&)':
/home/user/FolderMain/Projects_Test/DeviceLib/build-Device-jpDesktop_Qt_5_15_0_GCC_64bit-Debug/../Device/Device.cpp:25: undefined reference to `libusb_bulk_transfer'
/usr/bin/ld: /home/user/FolderMain/Projects_Test/DeviceLib/build-Device-jpDesktop_Qt_5_15_0_GCC_64bit-Debug/../Device/Device.cpp:26: undefined reference to `libusb_error_name'
/usr/bin/ld: /home/user/FolderMain/Projects_Test/DeviceLibTest/DeviceLibTest/DeviceLib//libDevice.a(Device.o): in function `Device::releaseDevice()':
/home/user/FolderMain/Projects_Test/DeviceLib/build-Device-jpDesktop_Qt_5_15_0_GCC_64bit-Debug/../Device/Device.cpp:31: undefined reference to `libusb_release_interface'
/usr/bin/ld: /home/user/FolderMain/Projects_Test/DeviceLib/build-Device-jpDesktop_Qt_5_15_0_GCC_64bit-Debug/../Device/Device.cpp:32: undefined reference to `libusb_close'
/usr/bin/ld: /home/user/FolderMain/Projects_Test/DeviceLib/build-Device-jpDesktop_Qt_5_15_0_GCC_64bit-Debug/../Device/Device.cpp:33: undefined reference to `libusb_exit'
/usr/bin/ld: /home/user/FolderMain/Projects_Test/DeviceLib/build-Device-jpDesktop_Qt_5_15_0_GCC_64bit-Debug/../Device/Device.cpp:35: undefined reference to `libusb_error_name'
collect2: error: ld returned 1 exit status
make: *** [Makefile:279: DeviceLibTest] Error 1
11:51:31: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project DeviceLibTest (kit: jpDesktop Qt 5.15.0 GCC 64bit)
When executing step "Make"

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)