PDA

View Full Version : Linux, libusb and Qt



jacaz4
29th July 2015, 17:55
Hi,
I want to use libusb in Qt Creator on linux mint.

I added libusb as external library.
in .pro file:


QT += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = test_libusb
TEMPLATE = app


SOURCES += main.cpp\
mainwindow.cpp

HEADERS += mainwindow.h

FORMS += mainwindow.ui

unix:!macx: LIBS += -L$$PWD/../../../../../../usr/local/lib/ -lusb-1.0

INCLUDEPATH += $$PWD/../../../../../../usr/local/include
DEPENDPATH += $$PWD/../../../../../../usr/local/include

unix:!macx: PRE_TARGETDEPS += $$PWD/../../../../../../usr/local/lib/libusb-1.0.a


in main.cpp:


#include "mainwindow.h"
#include <QApplication>
#include <libusb-1.0/libusb.h>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;

libusb_context ctx;

libusb_init( &ctx );


w.show();

return a.exec();
}


but, when I compile project, I get error:

../test_libusb/main.cpp:10:20: error: aggregate 'libusb_context ctx' has incomplete type and cannot be defined

Can you help? Please.

anda_skoa
29th July 2015, 19:06
libusb_context is just forward declared in libusb.h, it is an opaque data structure that you cannot create simply by direct instantiation.

Cheers,
_

Radek
29th July 2015, 19:35
libusb is a C library. Moreover, libusb-1.0/libusb.h is on the INCLUDE path. Therefore:


extern "C"
{
#include <libusb-1.0/libusb.h>
}

and you do not need to add INCLUDEPATH and DEPENDPATH to your profile. Next, libusb-1.0.so is on the PATH, therefore:


LIBS += -lusb-1.0

is enough.

It should compile. Another troubles ahead: libusb_open() needs root priviledges so that you will need to run your app using kdesu or gtksu. Byebye debugging using qtcreator or even running your app from the qtcreator. Oh well ...

jacaz4
29th July 2015, 19:52
Thanks for your answer, but I still get error (first post).
So how I should use this library?

Radek
29th July 2015, 20:33
Then the problem must be elsewhere. Does mainwindow.h include libusb.h, too? Are extern "C" brackets around all #includes of libusb.h? It should compile :) I have written a libusb-qt project myself, too.

jacaz4
29th July 2015, 20:44
mainwindow.h


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
extern "C"
{
#include <libusb-1.0/libusb.h>
}

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private:
Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

mainwindow.cpp


#include "mainwindow.h"
#include "ui_mainwindow.h"

extern "C"
{
#include <libusb-1.0/libusb.h>
}

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
delete ui;
}


main.c


#include "mainwindow.h"
#include <QApplication>

extern "C"
{
#include <libusb-1.0/libusb.h>
}

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;


libusb_context ctx;

libusb_init( &ctx );


w.show();

return a.exec();
}


test_libusb.pro


QT += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = test_libusb
TEMPLATE = app


SOURCES += main.cpp\
mainwindow.cpp

HEADERS += mainwindow.h

FORMS += mainwindow.ui


LIBS += -lusb-1.0

ChrisW67
29th July 2015, 21:30
The libusb_init() function creates a libusb_context and returns a pointer-to-libusb_context through its pointer-to-pointer argument. You are passing an invalid argument and creating the wrong target to hold it:


libusb_context *ctx;
libusb_init( &ctx );

You must, as a result, also call libusb_exit() before leaving the program.

jacaz4
29th July 2015, 22:13
It work!
Thank you very much!

anda_skoa
30th July 2015, 08:27
Thanks for your answer, but I still get error (first post).

If you ignore the answers that tell you what the problem is but instead follow answers that have nothing to do with the problem, why do you expect anything to change?

Magic? Some god awarding you for your effort regardless their futility?

Cheers,
_