我正在尝试使用libusb的 C 示例,并且在我尝试执行以下操作时一切正常:
libusb_device_descriptor descriptor;
int result = libusb_get_device_descriptor(usb_device, &descriptor);
编译器告诉我,在同一个libusb.h头文件中定义的某些元素,与我正在使用的其他 libusb 结构一样,没有声明。什么?
这是完整的代码:
#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!");
return EXIT_FAILURE;
}
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!");
return EXIT_FAILURE;
}
char message[24];
sprintf(message, "%d USB Devices found", (int)device_count);
puts(message);
ssize_t i;
for (i = 0; i < device_count; i++) {
}
libusb_free_device_list(usb_devices, 1);
libusb_exit(usb_context);
return EXIT_SUCCESS;
}
/**
*
*/
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");
return;
}
}
如果我离开/注释掉 printdev 函数,代码编译,并且 main() 运行完美。但是为什么在 main 中使用了结构,比如定义了 libusb_device 和 libusb_context,但 libusb_device_descriptor 没有?
这是我的编译器输出:
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)