Results 1 to 9 of 9

Thread: Linux, libusb and Qt

  1. #1
    Join Date
    Jul 2015
    Posts
    4
    Thanks
    3

    Default Linux, libusb and Qt

    Hi,
    I want to use libusb in Qt Creator on linux mint.

    I added libusb as external library.
    in .pro file:
    Qt Code:
    1. QT += core gui
    2.  
    3. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    4.  
    5. TARGET = test_libusb
    6. TEMPLATE = app
    7.  
    8.  
    9. SOURCES += main.cpp\
    10. mainwindow.cpp
    11.  
    12. HEADERS += mainwindow.h
    13.  
    14. FORMS += mainwindow.ui
    15.  
    16. unix:!macx: LIBS += -L$$PWD/../../../../../../usr/local/lib/ -lusb-1.0
    17.  
    18. INCLUDEPATH += $$PWD/../../../../../../usr/local/include
    19. DEPENDPATH += $$PWD/../../../../../../usr/local/include
    20.  
    21. unix:!macx: PRE_TARGETDEPS += $$PWD/../../../../../../usr/local/lib/libusb-1.0.a
    To copy to clipboard, switch view to plain text mode 

    in main.cpp:
    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QApplication>
    3. #include <libusb-1.0/libusb.h>
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication a(argc, argv);
    8. MainWindow w;
    9.  
    10. libusb_context ctx;
    11.  
    12. libusb_init( &ctx );
    13.  
    14.  
    15. w.show();
    16.  
    17. return a.exec();
    18. }
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Linux, libusb and Qt

    libusb_context is just forward declared in libusb.h, it is an opaque data structure that you cannot create simply by direct instantiation.

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    jacaz4 (30th July 2015)

  4. #3
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    256
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Linux, libusb and Qt

    libusb is a C library. Moreover, libusb-1.0/libusb.h is on the INCLUDE path. Therefore:
    Qt Code:
    1. extern "C"
    2. {
    3. #include <libusb-1.0/libusb.h>
    4. }
    To copy to clipboard, switch view to plain text mode 
    and you do not need to add INCLUDEPATH and DEPENDPATH to your profile. Next, libusb-1.0.so is on the PATH, therefore:
    Qt Code:
    1. LIBS += -lusb-1.0
    To copy to clipboard, switch view to plain text mode 
    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 ...

  5. The following user says thank you to Radek for this useful post:

    jacaz4 (29th July 2015)

  6. #4
    Join Date
    Jul 2015
    Posts
    4
    Thanks
    3

    Default Re: Linux, libusb and Qt

    Thanks for your answer, but I still get error (first post).
    So how I should use this library?

  7. #5
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    256
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Linux, libusb and Qt

    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.

  8. #6
    Join Date
    Jul 2015
    Posts
    4
    Thanks
    3

    Default Re: Linux, libusb and Qt

    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. extern "C"
    6. {
    7. #include <libusb-1.0/libusb.h>
    8. }
    9.  
    10. namespace Ui {
    11. class MainWindow;
    12. }
    13.  
    14. class MainWindow : public QMainWindow
    15. {
    16. Q_OBJECT
    17.  
    18. public:
    19. explicit MainWindow(QWidget *parent = 0);
    20. ~MainWindow();
    21.  
    22. private:
    23. Ui::MainWindow *ui;
    24. };
    25.  
    26. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 
    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. extern "C"
    5. {
    6. #include <libusb-1.0/libusb.h>
    7. }
    8.  
    9. MainWindow::MainWindow(QWidget *parent) :
    10. QMainWindow(parent),
    11. ui(new Ui::MainWindow)
    12. {
    13. ui->setupUi(this);
    14. }
    15.  
    16. MainWindow::~MainWindow()
    17. {
    18. delete ui;
    19. }
    To copy to clipboard, switch view to plain text mode 
    main.c
    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QApplication>
    3.  
    4. extern "C"
    5. {
    6. #include <libusb-1.0/libusb.h>
    7. }
    8.  
    9. int main(int argc, char *argv[])
    10. {
    11. QApplication a(argc, argv);
    12. MainWindow w;
    13.  
    14.  
    15. libusb_context ctx;
    16.  
    17. libusb_init( &ctx );
    18.  
    19.  
    20. w.show();
    21.  
    22. return a.exec();
    23. }
    To copy to clipboard, switch view to plain text mode 
    test_libusb.pro
    Qt Code:
    1. QT += core gui
    2.  
    3. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    4.  
    5. TARGET = test_libusb
    6. TEMPLATE = app
    7.  
    8.  
    9. SOURCES += main.cpp\
    10. mainwindow.cpp
    11.  
    12. HEADERS += mainwindow.h
    13.  
    14. FORMS += mainwindow.ui
    15.  
    16.  
    17. LIBS += -lusb-1.0
    To copy to clipboard, switch view to plain text mode 
    Last edited by jacaz4; 29th July 2015 at 20:59.

  9. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,552
    Thanks
    13
    Thanked 1,579 Times in 1,508 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Linux, libusb and Qt

    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:
    Qt Code:
    1. libusb_context *ctx;
    2. libusb_init( &ctx );
    To copy to clipboard, switch view to plain text mode 
    You must, as a result, also call libusb_exit() before leaving the program.

  10. The following user says thank you to ChrisW67 for this useful post:

    jacaz4 (29th July 2015)

  11. #8
    Join Date
    Jul 2015
    Posts
    4
    Thanks
    3

    Default Re: Linux, libusb and Qt

    It work!
    Thank you very much!

  12. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Linux, libusb and Qt

    Quote Originally Posted by jacaz4 View Post
    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,
    _

Similar Threads

  1. Libusb device Connection In Qt-Android-Emulator
    By saad_saadi in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 30th June 2015, 02:53
  2. Using libusb with QT Creator in Windows
    By dching in forum Newbie
    Replies: 1
    Last Post: 5th August 2014, 21:37
  3. Replies: 6
    Last Post: 7th November 2012, 05:13
  4. linux, libusb, and usb.h include issue
    By Windsoarer in forum Qt Programming
    Replies: 1
    Last Post: 4th July 2012, 09:21
  5. Replies: 7
    Last Post: 3rd December 2009, 16:23

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.