josef.van.niekerk josef.van.niekerk - 1 year ago 130
C Question

libusb semi-working, but libusb_device_descriptor undeclared?

I'm trying a C sample with libusb and things were working fine up to the point where I'm trying to do the following:

libusb_device_descriptor descriptor;
int result = libusb_get_device_descriptor(usb_device, &descriptor);

The compiler is telling me, that some elements defined in the same libusb.h header, as other libusb structs I am using, is not declared. WTH?

Here's the code in full:

#include <stdio.h>
#include <stdlib.h>
#include <libusb.h>

void printdev(libusb_device *usb_device);

* main
int main(void) {
puts("USB Test v0.0.1");

libusb_device **usb_devices;
libusb_context *usb_context = NULL;
int result;
result = libusb_init(&usb_context);
ssize_t device_count;

if (result < 0) {
puts("USB initialization error!");

libusb_set_debug(usb_context, 1);
device_count = libusb_get_device_list(usb_context, &usb_devices);

if (device_count < 0) {
puts("Unable to get USB device list!");

char message[24];
sprintf(message, "%d USB Devices found", (int)device_count);

ssize_t i;
for (i = 0; i < device_count; i++) {


libusb_free_device_list(usb_devices, 1);


void printdev(libusb_device *usb_device)
libusb_device_descriptor descriptor;
int result = libusb_get_device_descriptor(usb_device, &descriptor);

if (result < 0) {
puts("Failed to get device descriptor");

If I leave/comment out the printdev function, code compiles, and main() runs perfectly. But why on earth are structs used in main, like libusb_device and libusb_context defined, but libusb_device_descriptor is not?

Here's my compiler output:

23:18:07 **** Incremental Build of configuration debug for project usb_test ****
make all
Building file: ../src/usb_test.c
Invoking: GCC C Compiler
gcc -I/usr/local/Cellar/libusb/1.0.9/include/libusb-1.0 -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/usb_test.d" -MT"src/usb_test.d" -o "src/usb_test.o" "../src/usb_test.c"
../src/usb_test.c: In function 'printdev':
../src/usb_test.c:52: error: 'libusb_device_descriptor' undeclared (first use in this function)
../src/usb_test.c:52: error: (Each undeclared identifier is reported only once
../src/usb_test.c:52: error: for each function it appears in.)
../src/usb_test.c:52: error: expected ';' before 'descriptor'
../src/usb_test.c:53: error: 'descriptor' undeclared (first use in this function)
make: *** [src/usb_test.o] Error 1

23:18:07 Build Finished (took 73ms)

Answer Source

I have studied the Libusb.h as well, and the libusb_config_descriptor is not type defined, so you would have to type struct before libusb_config_descriptor.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download