Dynamic Libraries for OSX addon


#1

I ve made an addon for real time audio analysis wrapping the Essentia library (http://essentia.upf.edu/).

Here is the repo: https://github.com/leozimmerman/ofxAudioAnalyzer

The thing is that Essentia uses a lot of audio libraries. Ive tried to design the addon so you dont need to install all the libraries in the system, but Im having some problems with the dylib files.
When trying to run examples in a pc with no libraries install on the system this error shows:

dyld: Library not loaded: /usr/local/Cellar/ffmpeg/2.1.4/lib/libavfilter.3.dylib
Referenced from: /usr/lib/libavdevice.55.5.100.dylib
Reason: image not found
(lldb)

It seems to be searching the libraries in the systems directory.
Does anyone know if its possible to link this libraries from the addon directory without need to install them on the system?


#2

Hi @lzmmrman. I’m not exactly sure what you’re trying to do, but here’s some reading on paths for linking dynamic libraries.

https://wincent.com/wiki/@executable_path,@load_path_and@rpath

https://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/DynamicLibraries/100-Articles/RunpathDependentLibraries.html#//apple_ref/doc/uid/TP40008306-SW1

These were helpful to me when I was dealing with some Framework/dylib stuff from the Myo SDK, which also has some helpful info:

https://developer.thalmic.com/docs/api_reference/platform/the-sdk.html#building-cxx-apps-macosx


#3

Hi mattfelsen, thanks for the info, I think it solves my problem.
The thing i was wondering is if it is possible to use dynamic libraries (dylib files) in an Easy to Use addon.

Ive succesfully wrapped the Essentia audio library (http://essentia.upf.edu/) with OF:
https://vimeo.com/129795472
https://github.com/leozimmerman/ofxAudioAnalyzer

The issue is that you need to install de Essentia libraries and dependencies in the system in order to use the addon. (Xcode project is linked to /usr/local/libs)
I wanted library files in the addon directory to avoid Essentia installation, but if its complicated to link them in every new project maybe its better to leave as it is.


#4

Oh, sorry, I thought you were trying to link against a library installed on the user’s system (in /Library/Frameworks for example), but now I see you mean that you want to package the library with the addon so that the user doesn’t have to install anything other than the addon itself.

It is definitely possible to package the pre-compiled libraries. Generally libs for OF addons are compiled as static (.a) rather than dynamic (.dylib, .dll) – I’m not sure if there’s a reason behind that or it’s just become the norm – but I don’t see why you couldn’t package them as dynamic libs as well. There’s info here on the file structure to use to package libs for multiple platforms, and a nice example:


http://www.ofxaddons.com/pages/howto

Also pinging @theborg who has been interested in Essential as well:
https://github.com/GiantSteps/Essentia-Libraries/issues/1


#5

Sorry to re-open this old thread.
I am trying to use a dynamic library (.dylib) on osx and I am not sure how to write a proper addon_config.mk.
Could someone give me an example how to do this? I did not find any documentation dealing with dynamic libraries. Using a static lib is not an option.

Thanks
Thomas


#6

I hope this can help you.


#7

Thanks for the link, it is not really what i am looking for. I am having an external dylib, I want to bundle with an addon and use in my app.


#8

I am getting the following error:

dyld: Library not loaded: @rpath/libndi.dylib
  Referenced from: /Users/thomasgeissl/projects/dlr/openFrameworks/addons/ofxNDI/example-receiveVideo/bin/example-receiveVideo_debug.app/Contents/MacOS/example-receiveVideo_debug
  Reason: image not found

I am using Qt creator, 0.9.8. and/or the makefiles.

I tried adding
ADDON_LDFLAGS += -lndi -L/path/to/lib
and
ADDON_LIBS += /path/to/lib/libndi.dylib

Any pointers?


#9

dylibs can be tricky if they are compiled to be loaded from a specific location.
In this case though it looks fine.

I think the only thing you are missing is to add the dylib to the copy files build phase of the Xcode project.

Something similar to this:

But with the dylib being added instead of a framework.
You might also need to change the destination from frameworks to executable path.

If neither work - you might need to add a definition for rpath ( which is compiled into the dylib ) as described here:
http://www.dribin.org/dave/blog/archives/2009/11/15/rpath/


#10

Thanks @theo, it works fine when generating the project via the project generator and using xcode. But qt creator and the makefiles did not work. i will check the link you have provided.

The same happend to me when trying to compile an ofxDarknet example via the makefiles. https://github.com/mrzl/ofxDarknet


#11

Now it works when i run the app from xcode, but if i simply double click it in finder, it gives me the same error i get when i compile it with the make files. i try to fix this next week, if anyone has a solution please let me know.
Thanks
Thomas


#12

I’m running into a very similar situation.
I’m trying to put together a very basic addon that uses a .dylib on OSX.
I’ve used ADDON_LDFLAGS to set the addons relative path to the .dylib file,
but when I run otool -L on the executable I can see it’s still linking against the absolute system path (/usr/local/lib) instead of the of addon relative path.

Any tips on setting up addon_config.mk to link against an addon local .dylib instead of a system one ?

Thank you,
George


#13

This is absolutely one of pain when we make addon for OSX with dylib.

For standalone app package, we need to copy dylib and use install_name_tool.
(following is how oF manage libfmod.dylib in Xcode project ->Run Script phase)

rsync -aved "$OF_PATH/libs/fmodex/lib/osx/libfmodex.dylib" "$TARGET_BUILD_DIR/$PRODUCT_NAME.app/Contents/Frameworks/";
install_name_tool -change @executable_path/libfmodex.dylib @executable_path/../Frameworks/libfmodex.dylib "$TARGET_BUILD_DIR/$PRODUCT_NAME.app/Contents/MacOS/$PRODUCT_NAME";

Another option is this simple but super cool tool dylibbundler
dylibbundler automatically resolves shared lib dependency and copy dylib to inside of .app package. I tested and works amazing.

Here is sample command

$ cd yourCoolProject/bin
$ dylibbundler -od -b -x ./yourCoolProject.app/Contents/MacOS/yourCoolProject -d ./yourCoolProject.app/Contents/libs

You will be asked where is libfmodex.dylib but we can simply pass relative path like below.

'$ ./yourCoolProject.app/Contents/Frameworks'

*Of course please install dylibbunder before execute command above.