2

I am running c/c++ on Ubuntu and trying to compile the following code

#include <cassert>
#include <cstdio>
#include <libusb-1.0/libusb.h>

int main() {
    libusb_context *context = NULL;
    libusb_device **list = NULL;
    int rc = 0;
    ssize_t count = 0;

    rc = libusb_init(&context);
    assert(rc == 0);

    count = libusb_get_device_list(context, &list);
    assert(count > 0);

    for (size_t idx = 0; idx < count; ++idx) {
        libusb_device *device = list[idx];
        libusb_device_descriptor desc = {0};

        rc = libusb_get_device_descriptor(device, &desc);
        assert(rc == 0);

        printf("Vendor:Device = %04x:%04x\n", desc.idVendor, desc.idProduct);
    }
}

I get the following error when after I compile my code. Don’t have any idea what should I do?

/tmp/ccESLZ0k.o: In function `main':
libusbtest.cpp:(.text+0x2f): undefined reference to `libusb_init'
libusbtest.cpp:(.text+0x64): undefined reference to `libusb_get_device_list'
libusbtest.cpp:(.text+0xd4): undefined reference to `libusb_get_device_descriptor'
collect2: ld returned 1 exit status

I am a novice user of Ubuntu, c/c++ and libusb, so any help would be appreciated

Thanks

Sharif
  • 708
  • 1
  • 7
  • 17
Amjad
  • 1,950
  • 5
  • 26
  • 41

2 Answers2

3

This is a linker error.

You need to tell the linker to include libusb, which contains these referenced functions (e.g. -lusb) and also where it is (e.g. -L/usr/local/lib). Actual values will depend on your installation.

As Avidanborisov's answer highlights, you can use the tool to determine the linker flags. On my system this looks like:

% pkg-config --libs libusb-1.0                                    
-L/usr/local/Cellar/libusb/1.0.9/lib -lusb-1.0

You can feed this information directly to :

% g++ libusbtest.cpp $(pkg-config --libs libusb-1.0) -o libusbtest

Assuming all goes according to plan, you should now have an executable file libusbtest in your current working directory. You can run it like this:

% ./libusbtest                                                    
Vendor:Device = 05ac:8006
Vendor:Device = 05ac:8006
Vendor:Device = 05ac:8005
Vendor:Device = 05ac:8005
Vendor:Device = 05ac:850a
Vendor:Device = 05ac:023f
Vendor:Device = 05ac:8403
Community
  • 1
  • 1
johnsyweb
  • 136,902
  • 23
  • 188
  • 247
  • how to link any code example because as i mentioned i am a novice user so if you could tell me what command and or if you know any website that can help me to understand these linker errors ... thanks – Amjad Feb 24 '13 at 03:23
  • @UsmanSharifAmjadKhan: This will depend on which linker you're using. – johnsyweb Feb 24 '13 at 03:25
  • as I said I am a novice, therefore I have installed both libusb-1.0-dev and libusb-dev... and I am not sure what linkers are? – Amjad Feb 24 '13 at 11:01
  • linker error? do you mean by these are something to do with operating system or c or c++ or libusb... and how do i know which linker i am using? – Amjad Feb 24 '13 at 11:18
  • @UsmanSharifAmjadKhan: [This answer](http://stackoverflow.com/a/311889/78845) may help with explaining what a linker is and does. Most people use the linker that comes with their compiler. – johnsyweb Feb 24 '13 at 20:56
  • thank you very much as I am getting some errors when compiling that which I have mentioned below to @Avidanborisov... if you can help... thanks – Amjad Feb 24 '13 at 21:09
  • @UsmanSharifAmjadKhan: You're welcome. Don't forget [to mark helpful answers as accepted.](http://stackoverflow.com/faq/#howtoask) – johnsyweb Feb 24 '13 at 23:01
2

Use pkg-config to get the compiler flags needed for the library:

g++ libusbtest.cpp `pkg-config --libs libusb` -o libusbtest
Avidan Borisov
  • 3,235
  • 4
  • 24
  • 27
  • 1
    Note that for libusb-1.0 ,you'd use pkg-config --libs libusb-1.0 – nos Feb 24 '13 at 14:11
  • thanks @Avidanborisov i tried the above command with (g++ libusbtest.cpp 'pkg-config --libs libusb-1.0' -o libusbtest) but a compiler error (or error dont know) which says "g++ libusbtest.cpp 'pkg-config --libs libusb-1.0' -o libusbtest"... can you please help me with this again please... thanks for your time anyway...:) – Amjad Feb 24 '13 at 21:07
  • You need to use backticks, not single quotes, around the `pkg-config` command. Alternatively: `g++ libusbtest.cpp $(pkg-config --libs libusb) -o libusbtest`. – johnsyweb Feb 24 '13 at 21:12
  • thanks very very much, I ran the above command it ran did not show anything but problem is it is not printing anything... as you can see I am printing vendor id and device id but nothing printed... I am sorry if I am bugging you.... how would I know if i got what i wanted from this code?....:) thanks – Amjad Feb 24 '13 at 21:26
  • @UsmanSharifAmjadKhan: By ***Running*** the built executable. – johnsyweb Feb 24 '13 at 21:49