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
Use pkg-config
to get the compiler flags needed for the library:
g++ libusbtest.cpp `pkg-config --libs libusb` -o libusbtest