Using system or external libraries in a juce app (libusb, libudev)


#1

I am rewriting an app in JUCE that communicates over usb using several libraries that I didn’t write.

specifically it is a Midi output app for the snyderphonics manta.

http://www.snyderphonics.com/products.htm

it uses a manta specific library libmanta, which incorporates the crossplatform library hidapi, (which are both included as source in my app) which in turn relies on platform specific usb and hid libraries, which are not part of my app.

I’ve made basically the same app before on mac using swift, xcode and libmanta, and i’ve had no major problems with getting a basic new version working using juce and c++ on mac.

however i’ve come very unstuck trying to get the same app working on linux.

the hidapi (which is incorporated in my project as source) requires libusb, and I have it installed on my linux system, but the linker can’t find it.

edit - i’m using linux mint 17.3 -and g++ 4.8 since that’s what was in the repo.

i think this may be something to do with how the introjucer creates the make file. I don’t understand linux make files at all yet, and I was hoping that the introjucer would take care of it for me.

my libusb.h file is at /usr/include/libusb-1.0/libusb.h

my include statement in hid.c reads #include <libusb-1.0/libusb.h> since some research seems to indicate the versioned include is necessary.

although the original version in hidapi was just #include <libusb.h>

from some searching I think that maybe I need to add libusb to the external libraries to link field in the introjucer, but my attempts to do so so far have only resulted in libusb not found type errors.

do I actually need to learn all about linux gcc g++ makefiles and fix it separately from the introjucer or is there a way to get the introjucer to create a makefile that can find the library?

I’m using juce so that I can write basically the same code for mac and linux, so having to learn all the details of the platform specific stuff would defeat the point a bit.

hidapi also provides an option to use libudev instead of libusb - but I haven’t had any more luck with that, i still get errors that amount to not finding the library.


#2

This example is for Mac but the same principles apply to other targets.

Add the library name to the “External libraries to link” property in the target:

In this case I was using curl, the library file is called libcurl.a — so in terms of what you enter into the Projucer strip off “lib” from the the front and remove the file extension.

And add the search paths for the headers and the lib in each of the targets configurations (Debug and Release):

If some of your files pull the header at #include <libusb.h> and some #include <libusb-1.0/libusb.h> then you might need to add both of these to your header search paths:
/usr/include/libusb-1.0
/usr/include

Alternatively you could add the full path of the library (including the lib prefix and the file extension) to the “Extra linker flags” section of the target properties:

But you’ll still need to provide header seach paths for the code to compile.


How to deal with inter-dependencies with jucer projects?
#3

thanks that’s a brilliant guide, there’s stuff there i would never have guessed like stripping lib from the beginning of the name and putting a path in linker flags.

I still haven’t got it working - although I think I’m now closer…

if i use the linker flag path like this: /usr/lib/x86_64-linux-gnu/libusb-1.0a i get “no such file or directory” which is rubbish since the file is definitely there. although i’m not 100% sure that’s the version of the lib i should be using, or that there isn’t another libusb-1.0 in another directory somewhere! (but it’s not directly in usr/lib)

if i don’t use the linker flag field and adjust the search paths ie /usr/lib/x86_64-linux-gnu/ in library search paths and /usr/include/libusb-1.0 in header search paths, then i get a page of errors like this:

make
Linking MantaJuce
build/intermediate/Debug/hid_bf15222d.o: In function `libusb_get_string_descriptor':
/usr/include/libusb-1.0/libusb.h:1749: undefined reference to `libusb_control_transfer'
build/intermediate/Debug/hid_bf15222d.o: In function `make_path':
/home/minmin/Dev/Juce/MantaJuce/Builds/LinuxMakefile/../../Source/core/extern/hidapi/libusb/hid.c:402: undefined reference to `libusb_get_device_address'

and so on for maybe every function in the lib…

so something still isn’t connected up right…


#4

Ah I had specified usb instead of usb-1.0 in external libraries to link… so it was using the old wrong version.

it works now! (well it compiles and runs anyway)

thanks so much, your guide above should be published somewhere as part of the introjucer documentation or manual (is there a manual?? I haven’t found one.)


#5

I just made a convenient JUCE wrapper for the HID API, not sure if that would help here.