Linking libusb in Mac OS X

14,530

Solution 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

Solution 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.

Share:
14,530

Related videos on Youtube

Peter Mortensen
Author by

Peter Mortensen

Experienced application developer. Software Engineer. M.Sc.E.E. C++ (10 years), software engineering, .NET/C#/VB.NET (12 years), usability testing, Perl, scientific computing, Python, Windows/Macintosh/Linux, Z80 assembly, CAN bus/CANopen. Contact I can be contacted through this reCAPTCHA (requires JavaScript to be allowed from google.com and possibly other(s)). Make sure to make the subject specific (I said: specific. Repeat: specific subject required). I can not stress this enough - 90% of you can not compose a specific subject, but instead use some generic subject. Use a specific subject, damn it! You still don't get it. It can't be that difficult to provide a specific subject to an email instead of a generic one. For example, including meta content like "quick question" is unhelpful. Concentrate on the actual subject. Did I say specific? I think I did. Let me repeat it just in case: use a specific subject in your email (otherwise it will no be opened at all). Selected questions, etc.: End-of-line identifier in VB.NET? How can I determine if a .NET assembly was built for x86 or x64? C++ reference - sample memmove The difference between + and & for joining strings in VB.NET Some of my other accounts: Careers. [/]. Super User (SU). [/]. Other My 15 minutes of fame on Super User My 15 minutes of fame in Denmark Blog. Sample: Jump the shark. LinkedIn @PeterMortensen (Twitter) Quora GitHub Full jump page (Last updated 2021-11-25)

Updated on June 04, 2022

Comments

  • Peter Mortensen
    Peter Mortensen almost 2 years

    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;
    }
    
    • David Grayson
      David Grayson over 10 years
      Could you include the full output of GCC in each case so we can see the error messages? The error messages are like big clues that tell you how to fix the problem. Well, don't bother with the first invocation of GCC because there is no way that would work.
    • Admin
      Admin over 10 years
      @DavidGrayson sorry about that.
  • John Szakmeister
    John Szakmeister over 10 years
    He'll likely need to adjust his #include too if he points at just the include directory: #include <libusb-1.0/libusb.h>.
  • Admin
    Admin over 10 years
    @Mark This solved the missing file problem. Thank you. Btw... Here is the link homebrew created: lrwxr-xr-x 1 kyle admin 41 Dec 5 22:38 libusb-1.0 -> ../Cellar/libusb/1.0.9/include/libusb-1.0
  • mmmmmm
    mmmmmm over 10 years
    @KyleRogers which directory was that in?
  • Admin
    Admin over 10 years
    @Mark /usr/local/include
  • Admin
    Admin over 10 years
    @Mark I now get a different error. Any thoughts? Undefined symbols for architecture x86_64: "_libusb_get_device_list", referenced from: _main in usbtest-7VJQQT.o "_libusb_init", referenced from: _main in usbtest-7VJQQT.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
  • Admin
    Admin over 10 years
    @Mark I know this code works and is valid because I have it working with XCode. I'm just trying to learn a bit more about using gcc and command line.
  • mmmmmm
    mmmmmm over 10 years
    Well Xcode etc is using clang and not gcc - but that should not matter - as for that we need to see your makefile or whatever you are using to combine the results - there is a reason to use Xcode makes lfe much easier
  • geowar
    geowar over 6 years
    The "Undefined symbols" issue is a path issue also; just the library path (-L) instead of the include path (-I).