0

I'm trying to make use of LibUSB to interact with a small programmable LED device I've built. I have working code on Windows using the libusb-win32 port but I'm having trouble porting this to the MAC.

I've cloned LibUSB to a local git repository and compiled it using XCode I think is the preferred method on the MAC. After a bit of digging (including showing the hidden Library folder), it looks like XCode outputs to /Users/jon/Library/Developer/Xcode/DerivedData/libusb-ekndohtauywugtgjlolqmmdyoafq/Build/Products/Debug/libusb-1.0.0.dylib.

So my question is, how can I create a simple console application in XCode (or something else if there's an easier way of doing this) which links in that library and calls a function from the libusb library (e.g. libusb_init(libusb_context **ctx);)?

Jon Cage
  • 36,366
  • 38
  • 137
  • 215

4 Answers4

2

Rather than building libusb from scratch using Xcode you should probably just build it in the conventional way (as you would on e.g. Linux), or better yet, install it pre-built from Homebrew or MacPorts. Then just add the relevant header and library to your Xcode project in the normal way.

See also: How to set up libusb on Mac OS X?.

Community
  • 1
  • 1
Paul R
  • 208,748
  • 37
  • 389
  • 560
  • I should probably mention Most of my development experience to date has been on Windows. How would you do this in Linux? Or if I took the homebrew route, is that something that's easily deployed? – Jon Cage Nov 28 '14 at 08:49
  • I think the Homebrew route is probably your best bet - it's very simple to install, and then once you have it you can easily install any of thousands of useful packages. Just for fun I typed `sudo brew install libusb` and it was done in 4 seconds. The header is now installed at `/usr/local/include/libusb-1.0/libusb.h` and the libraries are under `/usr/local/lib`. – Paul R Nov 28 '14 at 08:53
  • 1
    Thanks; that sounds like a much easier solution. – Jon Cage Nov 28 '14 at 08:57
1

I have been frustrated for several days now trying to use libusb in my Xcode project. It started simple enough: 1. download libusb-1.0.20 from github; 2. run .configure, .make and .install - no errors; 3. drag libusb.dylib from /usr/local/lib into my project selecting copy options. Xcode automatically adds libusb.dylib to the Linked Frameworks and Libraries; 4. drag libusb.h from the build directory into my project

Project would build and run without errors until I submitted the app to the app store. Then I learned that dlopen is attempting to load from /usr/local/lib/libusb.dylib. Also, Xcode only copies the dylib file to my target bundle if I explicitly add it to the Copy Files Build Phase.

After reading through tons of documentation for something that should be easy, I find no simple way to have dlopen load the library from my app bundle. Store apps must be complete. They cannot users to already have libusb installed on their system.

Thanks, Bob Rice

Bob Rice
  • 11
  • 1
0
  1. Copy libusb.h into your project.
  2. Copy lib-1.0.0.dylib into your projects frameworks path list, set it "Required"
    In Xcode you will find under Project Settings > General the point Frameworks, Libraries, and Embedded Content which should list the lib-1.0.0.dylib by now.

As the filename suggests it is a dynamic library, means you load it at runtime.

This also implicates it does not need to be in the Project Settings > Build Phase > Link Binary with Libraries list. Because anything in there is usually static linked. If you place it there your linker will try to find it under the systems or usr library folder.

So if the lib is not installed - this will fail and your deployed dylib in YourApp/Contents/Frameworks is pretty much ignored. So there is no need to place it extra in Link Binary with Libraries

  1. instead load it dynamical and ask for the right folder to do so..
    (here for a Objective-C project)
#import "libusb.h"
#include <dlfcn.h>

// and at appropriate place in code
NSString *privateFrameworksPath = [[NSBundle mainBundle] privateFrameworksPath];
NSString *libpath = [privateFrameworksPath stringByAppendingString:@"/libusb-1.0.0.dylib"];
const char *dylibPath = libpath.UTF8String;
void *handle = dlopen(dylibPath, RTLD_LAZY);
...
dlclose(handle);
Ol Sen
  • 3,163
  • 2
  • 21
  • 30
0

In my case I've added headers from Build Settings -> Header Search Paths. Here you have to add the headers file. I'm using brew to manage packages so my was living here: /usr/local/Cellar/libusb/1.0.24/include/libusb-1.0

Then in Build Settings -> Framework Search Paths add the path to the library itself, something like: /usr/local/Cellar/libusb/1.0.24/lib

The same path you have to add to Library search paths.

Then in Build Phases -> Link Binary With Libraries, you click on the "+" to add files, Add Other -> Add Files, then navigate to the directory where your library lives and select the ".a" file, in my case "/usr/local/Cellar/libusb/1.0.24/lib" and selected "libusb-1.0.a" and I was able to build the project.

Dharman
  • 30,962
  • 25
  • 85
  • 135
eazybob
  • 109
  • 2
  • 10