简体   繁体   中英

problems compiling c code with libusb on linux

I'm trying to use libusb for a project but i'm unable to get the library working properly. Here is some source code i'm trying to compile. It doesn't do anything special. It's just a dummy program that gets the USB driver list then frees it.

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

int main(int argc, char *argv[]){

     struct libusb_device **devs;
     struct libusb_context *context = NULL;
     size_t list;
     size_t i;
     int ret;

   ret = libusb_init(&context);

   if(ret < 0)
      {
        perror("libusb_init");
        exit(1);
      }

   list = libusb_get_device_list(context, &devs);

   if(list < 0)
   {

       fprintf(stderr, "error in getting device list\n");
       libusb_free_device_list(devs, 1);
       libusb_exit(context);
       exit(1);
   }


   libusb_free_device_list(devs, 1);
   libusb_exit(context);

  return 0;

}

I compile with

gcc -o test test.c -lusb

I get the error

/tmp/cc2hwzii.o: in function 'main:
test.c:(.text+0x24): undefined reference to 'libusb_init'
test.c:(.text+0x59): undefined reference to 'libusb_get_device_list'
test.c:(.text+0x8e): undefined reference to 'libusb_free_device_list'
test.c:(.text+0x9f): undefined reference to 'libusb_exit'
collect2: error: ld returned 1 exit status

I'm running ubuntu 14.04.3 I've installed libusb by sudo apt-get install libusb-dev I've searched for my header file and it is called usb.h I've looked to make sure I have the correct flag and it's -lusb

any ideas? I'd appreciate the help. If any more information is needed just ask.

those libusb_init are included in libusb-1.0 . you have to install libusb-1.0-0-dev

$ sudo apt-get install libusb-1.0-0-dev
$ gcc -o test test.c -lusb-1.0

/usr/lib/x86_64-linux-gnu/pkgconfig/libusb.pc which is included in libusb-dev says that the version is 0.1.12

and /usr/lib/x86_64-linux-gnu/pkgconfig/libusb-1.0.pc which is included in libusb-1.0-0-dev says that the version is 1.0.17.

http://www.libusb.org/ says that 0.1 is legacy, and seems that API is different from 1.0.

You forgot to include the file that defines the functions, such as libusb_init. Have you tried including libusb.h?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM