GCC编译错误

发布于 2024-09-09 02:34:50 字数 7049 浏览 8 评论 0原文

当我尝试编译一个程序时,出现了以下情况:

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  

这是我的代码,上面的方法错误都是来自以下函数:

/*
 * 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(description), "%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;
}

我发现我的代码没有任何问题。有什么建议吗?

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(description), "%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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

少钕鈤記 2024-09-16 02:34:50

您需要在链接标志中包含 -lusb 才能链接到 libusb。如有必要,还请记住包含其库路径。

You need to include -lusb in the linking flags to link to libusb. Also remember to include its library path if neccessary.

孤千羽 2024-09-16 02:34:50

可能您错过了与其中定义了 USB 函数的库的链接。

当您使用 -I 选项传递正确的路径时,不存在与包含相关的错误。
但是,您必须将其与 -l 链接,并使用 -L 选项指定库的位置。

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.

双马尾 2024-09-16 02:34:50

您可能没有链接到这些库 libs(.so.a.dylib) - 查看文档并查看您使用的库需要将您的程序链接到。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文