Question

I've been attempting for the past 16 hours to attach the libusb library to a Qt project without much success. I would appreciate any input on the matter, it's getting frustrating.

The .pro file is this:

TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt

SOURCES += main.cpp

INCLUDEPATH +=/usr/local/include/libusb-1.0
LIBS += -L/usr/local/lib -libusb-1.0.a
LIBS += -L<libusb.h>

Source code:

#include <iostream>
#include <libusb.h>


using namespace std;

int main()
{
    cout << "Hello World!" << endl;
    return 0;
}

Compiler output:

13:01:50: Running steps for project lallala...
13:01:50: Configuration unchanged, skipping qmake step.
13:01:50: Starting: "/usr/bin/make" -w
make: Entering directory `/Users/MAXIMUS/Documents/workspace/lallala-build-Desktop_Qt_5_0_0_clang_64bit_SDK-Debug'
/Users/MAXIMUS/Qt5.0.0/5.0.0/clang_64/bin/qmake -spec macx-g++42 CONFIG+=debug CONFIG+=x86_64 CONFIG+=declarative_debug CONFIG+=qml_debug -o Makefile ../lallala/lallala.pro
make: Leaving directory `/Users/MAXIMUS/Documents/workspace/lallala-build-Desktop_Qt_5_0_0_clang_64bit_SDK-Debug'
make: Entering directory `/Users/MAXIMUS/Documents/workspace/lallala-build-Desktop_Qt_5_0_0_clang_64bit_SDK-Debug'
g++-4.2 -headerpad_max_install_names -mmacosx-version-min=10.6 -o lallala main.o   -L/usr/local/lib -libusb-1.0.a -L<libusb.h> 
/bin/sh: -c: line 0: syntax error near unexpected token `newline'
/bin/sh: -c: line 0: `g++-4.2 -headerpad_max_install_names -mmacosx-version-min=10.6 -o lallala main.o   -L/usr/local/lib -libusb-1.0.a -L<libusb.h> '
make: *** [lallala] Error 2
make: Leaving directory `/Users/MAXIMUS/Documents/workspace/lallala-build-Desktop_Qt_5_0_0_clang_64bit_SDK-Debug'
13:01:50: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project lallala (kit: Desktop Qt 5.0.0 clang 64bit (SDK))
When executing step 'Make'
Was it helpful?

Solution

Forming my comment into a proper answer; this is not the correct syntax to use:

LIBS += -L/usr/local/lib -libusb-1.0.a
LIBS += -L<libusb.h>

The proper one would be this:

LIBS += -L/usr/local/lib -lusb-1.0

or

LIBS += -l/full/path/to/libusb-1.0.a

You can drop the second LIBS line in your initial attempt because you have already specified the path in the former, and putting an "include" statement in there would not be reasonable anyhow. So, this is what you could write for your complete .pro file:

TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt

SOURCES += main.cpp

INCLUDEPATH +=/usr/local/include/libusb-1.0
LIBS += -L/usr/local/lib -lusb-1.0

This is not Qt specific, just generic linkage issue: -lfoo extends to $(prefix)foo$(suffix), where the prefix and suffix are figured out automatically based on the platform. That is, the prefix would be lib in your case, and suffix would be either .a or .so on Unix, probably .dylib on Mac, etc.

You may wish to look into pkg-config support if it is possible to establish. In that case, you would write something like this what we did in QtSerialPort:

CONFIG += link_pkgconfig
PKGCONFIG += libudev

Yet another option is to add the GUI through the QtCreator IDE or similar IDE that you may be using. There is an option usually in the "Linker" section to add a library. Here are two screenshots from my QtCreator:

enter image description here

enter image description here

Click on the project name on the left in the project source tree navigator, and select Add Library. Then the first screenshot will come up, and you can select the external option, and then you can see the second.

It is needless to say that you would need to run qmake after these changes to generate the corresponding Makefile on your desired platform.

OTHER TIPS

Syntax is the following: -L%LIBRARY_PATH% to make a specific path visible and -l%LIBRARY_NAME% to link a specific library that is located in a visible path

so I guess this should work (I don't think you need the .a extension):

LIBS += -L/usr/local/lib -llibusb-1.0

and I have no idea what this would do: LIBS += -L so I guess I'd remove it.

Once fixed run qmake then build...

Hope it helps...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top