Click here to Skip to main content
15,880,543 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to create a USB device information application using Qt in ubuntu OS. when I try to create a sample application. It gives the following error

main.o: In function `main':
main.cpp:(.text.startup+0x36): undefined reference to `libusb_init'
main.cpp:(.text.startup+0x52): undefined reference to `libusb_get_device_list'
make: Leaving directory `/home/arun/TestProgr-build-desktop-Qt_4_8_1_in_PATH__System__Release'
main.cpp:(.text.startup+0x74): undefined reference to `libusb_get_device_descriptor'
main.cpp:(.text.startup+0x84): undefined reference to `libusb_get_device_address'
main.cpp:(.text.startup+0x8e): undefined reference to `libusb_get_bus_number'
main.cpp:(.text.startup+0xe2): undefined reference to `libusb_free_device_list'
main.cpp:(.text.startup+0xee): undefined reference to `libusb_exit'
collect2: ld returned 1 exit status
make: *** [TestProgr] Error 1
15:32:24: The process "/usr/bin/make" exited with code 2.
Error while building project TestProgr (target: Desktop)

When executing build step 'Make'

Any one please help me to make it work.......

Code

C++
#include <QtCore/QCoreApplication>
#include <libusb-1.0/libusb.h>
#include <stdio.h>
#include <sys/types.h>

static void print_devs(libusb_device **devs)
{
    libusb_device *dev;
    int i = 0;

    while ((dev = devs[i++]) != NULL) {
        struct libusb_device_descriptor desc;
        int r = libusb_get_device_descriptor(dev, &desc);
        if (r < 0) {
            fprintf(stderr, "failed to get device descriptor");
            return;
        }

        printf("%04x:%04x (bus %d, device %d)\n",
            desc.idVendor, desc.idProduct,
            libusb_get_bus_number(dev), libusb_get_device_address(dev));
    }
}

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

    libusb_device **devs;
        int r;
        ssize_t cnt;

        r = libusb_init(NULL);
        if (r < 0)
            return r;

        cnt = libusb_get_device_list(NULL, &devs);
        if (cnt < 0)
            return (int) cnt;

        print_devs(devs);
        libusb_free_device_list(devs, 1);

        libusb_exit(NULL);

    return a.exec();
}




When I included the library.....

win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../../usr/local/lib/release/ -lusb-1 
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../../usr/local/lib/debug/ -lusb-1
else:symbian: LIBS += -lusb-1 
else:unix: LIBS += -L$$PWD/../../../usr/local/lib/ -lusb-1 
NCLUDEPATH += $$PWD/../../../usr/local/include 
DEPENDPATH += $$PWD/../../../usr/local/include


It showing cannot find the library
:-1: error: cannot find -lusb-1 

But that is exist in that path
Posted
Updated 16-Jan-13 23:46pm
v3
Comments
Richard MacCutchan 17-Jan-13 6:04am    
LIBS += -L$$PWD/../../../usr/local/lib/ -lusb-1
Are you sure that is the correct path for libusb-1?
Arun Kumar K S 17-Jan-13 7:49am    
LIBS += -L$$PWD/../../../usr/local/lib/ -lusb-1
Thi path is creating when browse the library from qt creators default library browsing wizard
This solved to me using this way
LIBS += /usr/local/lib/libusb-1.0.so

look below answer for more. He helped me to find the issue...
Richard MacCutchan 17-Jan-13 7:51am    
Exactly so.

1 solution

You must link your program with the libusb library.

Therefore, you have to add the libary name libusb to your project. How to do this depends on your development environment. With a simple Makefile, just add the name at the end of the linker call (many Makefiles use a LDLIBS variable that is passed to the linker).

[UPDATE]
The common syntax (gcc) for adding a library name in short form is '-l<name of lib>' where the leading 'lib' and the extension are not passed (library must be in a path known by the linker). In your case the parameter is then just '-lusb'. When not using the short form, the complete path and name must be passed behind switched arguments (e.g. '/usr/lib64/libusb.so').
 
Share this answer
 
v2
Comments
Arun Kumar K S 17-Jan-13 5:40am    
Thanks..., but still have some issues
Arun Kumar K S 17-Jan-13 5:42am    
I included the library.....

win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../../usr/local/lib/release/ -lusb-1
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../../usr/local/lib/debug/ -lusb-1
else:symbian: LIBS += -lusb-1
else:unix: LIBS += -L$$PWD/../../../usr/local/lib/ -lusb-1

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

It showing cannot find the library
:-1: error: cannot find -lusb-1
but that is exist in that path
Jochen Arndt 17-Jan-13 6:01am    
See also my updated answer. The common name of the library file is libusb.so (without '-1'). First try to locate the library and it's name. The '-L' option specifies pathes to libraries and the -l option library files that reside in one of the pathes (if there are multiple).
Arun Kumar K S 17-Jan-13 6:21am    
many thanks.............
It worked when I put like this
unix:!macx:!symbian: LIBS += /usr/local/lib/libusb-1.0.so
Jochen Arndt 17-Jan-13 6:30am    
Thank you for the feedback. The names and pathes of Linux files are sometimes not identical with different distributions (and even different versions of one distribution). In your case it is the name of the library file. It should be also possible to use 'LIBS+=L/usr/local/lib -lusb-1.0'.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900