When I tried to compile a program this came up:
C:\Users\Mohit\Developer\C_Workspace\iPhoneInteraction\Debug>make all
Building file: ../src/test.c
Invoking: Cygwin C Compiler
gcc -I"C:\cygwin\usr\include" -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"s
rc/test.d" -MT"src/test.d" -o"src/test.o" "../src/test.c"
cygwin warning:
MS-DOS style path detected: C:/cygwin/usr/include
Preferred POSIX equivalent is: /usr/include
CYGWIN environment variable option "nodosfilewarning" turns off this warning.
Consult the user's guide for more details about POSIX paths:
http://cygwin.com/cygwin-ug-net/using.html#using-pathnames
Finished building: ../src/test.c
Building target: iPhoneInteraction.exe
Invoking: Cygwin C Linker
gcc -o"iPhoneInteraction.exe" ./src/test.o
./src/test.o: In function print_device':
/cygdrive/c/Users/Mohit/Developer/C_Workspace/iPhoneInteraction/Debug/../src/tes
t.c:70: undefined reference to '_usb_open'
/cygdrive/c/Users/Mohit/Developer/C_Workspace/iPhoneInteraction/Debug/../src/tes
t.c:73: undefined reference to '_usb_get_string_simple'
/cygdrive/c/Users/Mohit/Developer/C_Workspace/iPhoneInteraction/Debug/../src/tes
t.c:84: undefined reference to '_usb_get_string_simple'
/cygdrive/c/Users/Mohit/Developer/C_Workspace/iPhoneInteraction/Debug/../src/tes
t.c:104: undefined reference to '_usb_get_string_simple'
/cygdrive/c/Users/Mohit/Developer/C_Workspace/iPhoneInteraction/Debug/../src/tes
t.c:112: undefined reference to '_usb_close'
./src/test.o: In function 'main':
/cygdrive/c/Users/Mohit/Developer/C_Workspace/iPhoneInteraction/Debug/../src/tes
t.c:137: undefined reference to '_usb_init'
/cygdrive/c/Users/Mohit/Developer/C_Workspace/iPhoneInteraction/Debug/../src/tes
t.c:139: undefined reference to '_usb_find_busses'
/cygdrive/c/Users/Mohit/Developer/C_Workspace/iPhoneInteraction/Debug/../src/tes
t.c:140: undefined reference to '_usb_find_devices'
/cygdrive/c/Users/Mohit/Developer/C_Workspace/iPhoneInteraction/Debug/../src/tes
t.c:142: undefined reference to '_usb_get_busses'
collect2: ld returned 1 exit status
make: *** [iPhoneInteraction.exe] Error 1
Here is my code, the method errors above were all functions from :
/*
* testlibusb.c
*
* Test suite program
*/
#include <stdio.h>
#include <string.h>
#include <usb.h>
int verbose = 0;
void print_endpoint(struct usb_endpoint_descriptor *endpoint)
{
printf(" bEndpointAddress: %02xh\n", endpoint->bEndpointAddress);
printf(" bmAttributes: %02xh\n", endpoint->bmAttributes);
printf(" wMaxPacketSize: %d\n", endpoint->wMaxPacketSize);
printf(" bInterval: %d\n", endpoint->bInterval);
printf(" bRefresh: %d\n", endpoint->bRefresh);
printf(" bSynchAddress: %d\n", endpoint->bSynchAddress);
}
void print_altsetting(struct usb_interface_descriptor *interface)
{
int i;
printf(" bInterfaceNumber: %d\n", interface->bInterfaceNumber);
printf(" bAlternateSetting: %d\n", interface->bAlternateSetting);
printf(" bNumEndpoints: %d\n", interface->bNumEndpoints);
printf(" bInterfaceClass: %d\n", interface->bInterfaceClass);
printf(" bInterfaceSubClass: %d\n", interface->bInterfaceSubClass);
printf(" bInterfaceProtocol: %d\n", interface->bInterfaceProtocol);
printf(" iInterface: %d\n", interface->iInterface);
for (i = 0; i < interface->bNumEndpoints; i++)
print_endpoint(&interface->endpoint[i]);
}
void print_interface(struct usb_interface *interface)
{
int i;
for (i = 0; i < interface->num_altsetting; i++)
print_altsetting(&interface->altsetting[i]);
}
void print_configuration(struct usb_config_descriptor *config)
{
int i;
printf(" wTotalLength: %d\n", config->wTotalLength);
printf(" bNumInterfaces: %d\n", config->bNumInterfaces);
printf(" bConfigurationValue: %d\n", config->bConfigurationValue);
printf(" iConfiguration: %d\n", config->iConfiguration);
printf(" bmAttributes: %02xh\n", config->bmAttributes);
printf(" MaxPower: %d\n", config->MaxPower);
for (i = 0; i < config->bNumInterfaces; i++)
print_interface(&config->interface[i]);
}
int print_device(struct usb_device *dev, int level)
{
usb_dev_handle *udev;
char description[256];
char string[256];
int ret, i;
udev = usb_open(dev);
if (udev) {
if (dev->descriptor.iManufacturer) {
ret = usb_get_string_simple(udev, dev->descriptor.iManufacturer, string, sizeof(string));
if (ret > 0)
snprintf(description, sizeof(description), "%s - ", string);
else
snprintf(description, sizeof(descri开发者_运维百科ption), "%04X - ",
dev->descriptor.idVendor);
} else
snprintf(description, sizeof(description), "%04X - ",
dev->descriptor.idVendor);
if (dev->descriptor.iProduct) {
ret = usb_get_string_simple(udev, dev->descriptor.iProduct, string, sizeof(string));
if (ret > 0)
snprintf(description + strlen(description), sizeof(description) -
strlen(description), "%s", string);
else
snprintf(description + strlen(description), sizeof(description) -
strlen(description), "%04X", dev->descriptor.idProduct);
} else
snprintf(description + strlen(description), sizeof(description) -
strlen(description), "%04X", dev->descriptor.idProduct);
} else
snprintf(description, sizeof(description), "%04X - %04X",
dev->descriptor.idVendor, dev->descriptor.idProduct);
printf("%.*sDev #%d: %s\n", level * 2, " ", dev->devnum,
description);
if (udev && verbose) {
if (dev->descriptor.iSerialNumber) {
ret = usb_get_string_simple(udev, dev->descriptor.iSerialNumber, string, sizeof(string));
if (ret > 0)
printf("%.*s - Serial Number: %s\n", level * 2,
" ", string);
}
}
if (udev)
usb_close(udev);
if (verbose) {
if (!dev->config) {
printf(" Couldn't retrieve descriptors\n");
return 0;
}
for (i = 0; i < dev->descriptor.bNumConfigurations; i++)
print_configuration(&dev->config[i]);
} else {
for (i = 0; i < dev->num_children; i++)
print_device(dev->children[i], level + 1);
}
return 0;
}
int main(int argc, char *argv[])
{
struct usb_bus *bus;
if (argc > 1 && !strcmp(argv[1], "-v"))
verbose = 1;
usb_init();
usb_find_busses();
usb_find_devices();
for (bus = usb_busses; bus; bus = bus->next) {
if (bus->root_dev && !verbose)
print_device(bus->root_dev, 0);
else {
struct usb_device *dev;
for (dev = bus->devices; dev; dev = dev->next)
print_device(dev, 0);
}
}
return 0;
}
There is nothing that I catch to be wrong with my code. Any suggestions?
You need to include -lusb
in the linking flags to link to libusb. Also remember to include its library path if neccessary.
Probably you have missed linking with the library which has the functions for usb defined in them.
There is no include related error as you are passing the correct path with the -I
option.
However you have to link it with the -l<libname>
and also specify the location of the library using the -L<libpath>
option.
You are probably not linking to those libraries libs (.so
or .a
or .dylib
) – look at the documentation and see what libs you need to link your program with.
精彩评论