Linking libusb in Mac OS X

匿名 (未验证) 提交于 2019-12-03 10:10:24

问题:

I have this very simple piece of code that I'm trying to compile. I'm fairly new to GCC from the command line, so please forgive me. I've tried a quite few different things with GCC, but I'm still unable to get it to compile. I do have libusb installed. How can I get this piece of code to compile?

Libusb:

anything:usb mymac$ brew list libusb /usr/local/Cellar/libusb/1.0.9/include/libusb-1.0/libusb.h /usr/local/Cellar/libusb/1.0.9/lib/libusb-1.0.0.dylib /usr/local/Cellar/libusb/1.0.9/lib/pkgconfig/libusb-1.0.pc /usr/local/Cellar/libusb/1.0.9/lib/ (2 other files) anything:usb mymac$ 

GCC attempts (all failed):

gcc -o xout usbtest.c gcc -o xout usbtest.c -lusb-1.0 gcc -L/usr/local/Cellar/libusb/1.0.9/lib -o xout usbtest.c -lusb-1.0 

Error for all attempts:

usbtest.c:3:10: fatal error: 'libusb.h' file not found #include <libusb.h> 

Code:

#include <stdio.h> #include <stdlib.h> #include <libusb.h>  int main(int argc, const char * argv[]) {     libusb_device **devs;     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);      printf("There are %zd devices found\n", list);      return 0; } 

回答1:

You are not telling gcc where to look for the header files. This is done by the -I option on the gcc command line for compiling.

e.g.

gcc -I /usr/local/include -o xout usbtest.c 

I think Homebrew does provide a symbolic link frominside the Cellar to /usr/local



回答2:

So I had a similar issue, for some reason gcc doesnt include /usr/local/lib in its default search path on OS X. The quick fix is to add:

-lusb-1.0 

to the gcc commands and it should compile.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!