0

Please help me. I can't use my static library in my DeviceTest project. Below is my static libDevice. In it I use 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;
}

New project (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();
}

Build DeviceTest project not successful. I am getting errors while building it.

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   

These are the errors I get. I added my library using the Qt Creator dialog window (Context menu --> add Library...) Errors: 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"
  • Isn't the linking line (`g++ -Wl,-rpath,/home/jp/Qt/5.15.0/gcc_64/lib -o DeviceLibTest main.o moc_Device.o -L/home/user/FolderMain/Projects_Test/DeviceLibTest/DeviceLibTest/DeviceLib/ -lDevice /home/jp/Qt/5.15.0/gcc_64/lib/libQt5Core.so -lpthread`) missing `-lusb`? – Ted Lyngmo Nov 05 '20 at 09:40
  • This output is generated by QtCreator – Илья Сонгров Nov 05 '20 at 09:42
  • Yes, and it seems like the `usb` lib is missing, doesn't it? – Ted Lyngmo Nov 05 '20 at 09:45
  • Sidenote: Don't do `-lpthread` - use `-pthread` instead. – Ted Lyngmo Nov 05 '20 at 09:46
  • Added "LIBS + = -lusb-1.0" to .pro file and get the same error. (Edited build output in the question) – Илья Сонгров Nov 05 '20 at 09:59
  • 1
    Your `Device` library uses functions in the `usb-1.0` library so the linking order should be the opposite to what you got now. Make it: `-lDevice -lusb-1.0` – Ted Lyngmo Nov 05 '20 at 10:24
  • @TedLyngmo I run this command (`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 -pthread`) in terminal and I get the same error. – Илья Сонгров Nov 05 '20 at 10:36
  • Yes, and you have the same order (the wrong order) on that line. Try: `g++ -Wl,-rpath,/home/jp/Qt/5.15.0/gcc_64/lib -o DeviceLibTest main.o moc_Device.o -L/home/user/FolderMain/Projects_Test/DeviceLibTest/DeviceLibTest/DeviceLib/ -lDevice -lusb-1.0 /home/jp/Qt/5.15.0/gcc_64/lib/libQt5Core.so -pthread` - note that I moved `-lusb-1.0` to _after_ `-lDevice`. – Ted Lyngmo Nov 05 '20 at 10:40
  • 1
    @TedLyngmo Thank you very match. I moved `-lusb-1.0` to after `-lDevice` and it worked. – Илья Сонгров Nov 05 '20 at 10:46
  • Lovely! You're welcome! I'll close this as a duplicate question now. I wasn't sure when I started commenting, but now it's obvious. – Ted Lyngmo Nov 05 '20 at 10:46

0 Answers0