views:

62

answers:

2

How can I reference a class/framework/library like libusb in an Objective-C class?

Currently I have tried to initiate an enum/struct-function from the library inside my @interface in my .h-file.
But that doesn't work, apparently. :/
I have it "installed" (it's in /usr/local), and tried adding both the files and as framework. Doesn't help, though.

A: 
  1. Copy libusb to your project.
  2. Add #include lines.
mcandre
A: 

Chances are that you haven't added /usr/local/include and /usr/local/lib to the search paths in your project. In Xcode, bring up either the project settings or the target settings and look for a section named "Search Paths". Double-click on "Header Search Paths" and add /usr/local/include. Now, double-click on "Library Search Paths" and add /usr/local/lib. When you rebuild your project, libusb should be available. You will need to add #include <libusb.h> at the top of each source file that uses functionality from that product.

Apple makes a distinction between libraries and frameworks. A library is what you have now. There are header files and library (dylib) files that you compile and link against. They are stored in somewhat global locations, such as /usr/include, /usr/local/include, and so-forth. Frameworks are bundles that contains headers, libraries, graphical resources, version information, and more.

Paul
But how can I declare a variable/device in the @implentation ? `static struct libusb_device_handle *device = NULL;` gives the following error: `xpected specifier-qualifier-list before 'static'`
Josso
I believe `libusb_device_handle` is a typedef. Remove the `struct` from that line and it will likely work as expected. If the compiler still cannot find the type, you probably don't have the `#include <libusb.h>` at the top of you implementation file.
Paul
I still get the same error. The compiler doesn't complain that the file <libusb-1.0/libusb.h> doesn't exist where as it does with <libusb.h>, so I guess I have it included.
Josso
That's because your compiler is looking at `/usr/local/include` and you need to specify `<libusb-1.0/...>` as the library installed its headers under a subdirectory with that name.What version of OS X are you compiling on? Which version of GCC or LLVM are you using? What do your project settings look like?
Paul
I've added /usr/local/include/libusb-1.0/ to the "Header Search Paths" but still won't work. Any chance of you uploading an example? (That'll probably be faster)
Josso
You should really add `/usr/local/include` to the header search path and add the last level into your include statement as: `#include <libusb-1.0/libusb.h>`Since you're the one asking the question, it would be helpful if you upload some source and your project settings, as was already requested.
Paul