How to link dylibs into plugin?

I’m able to link to some dylibs that we need in a stand-alone app, but when I try to do the same thing in a plugin, the plugin cannot be loaded. Using Logic’s Plug-In Manager, I can see this error: “Library not loaded: @rpath/libtorch.dylib”

No matter where I put the dylib, or what path I enter using -L in the Other Linker Flags, it does not seem to find the dylib.

What are the proper settings in the .jucer file that will allow Projucer to set up my Xcode such that the resulting plugins load in the various hosts?

I’ve added torch to the External Libraries to Link, and entered the various paths I’ve attempted to use, using -L in the Other Linker Flags. But no luck. Can someone help?

Hi HowardAntares,

well, if it’s a dylib it’s normal that changing the linker’s option doesn’t help, since they are dynamically linked to your vst each this one is executed. If you want your plugin to be loaded you need to put them in a folder where the os can find it.

Alexis

in a post build .sh
copy the dylib into your bundle Frameworks dir

then

install_name_tool -change MyOriginalDylibPath @loader_path/../Frameworks/MyDylib.dylib "$TARGET_BUILD_DIR/$PRODUCT_NAME$WRAPPER_SUFFIX/Contents/MacOS/$PRODUCT_NAME"

When building All, the Post-Build Shell Script appears to run on the Shared Code before any of the targets have even linked. How do I get it to only run on targets that actually create plugins? I tried this code (from a previous post where someone gave me a way to test if the target EQUALED something specific), but can’t seem to get the syntax correct for testing if it is NOT equal to that:

TARGET=echo $TARGET_NAME | sed 's/^.*- \(.*\).*$/\1/g'
if ! [ “$TARGET” = “Shared Code” ]; then …

If I put the ! before the =, I get an “unexpected end of file” error. If I put it here, before the [, I get “[: too many arguments”. I need to be able to run this script code only for targets that actually produce plugins, but how?

if [ “${TARGET}” != “Shared Code” ]; then

If I put the ! before the =, I get an “unexpected end of file” error. If I put it here, before the [, I get “[: too many arguments”.

The if [ "$TARGET" != "Shared Code" ]; then ... syntax should work, so if you’re still getting the unexpected end of file error the issue may be elsewhere in the script. Is it possible you’ve left out the closing fi keyword?

Oh, I see it. When I copied and pasted that code, somewhere along the way the quotes got turned into left and right quotes, which don’t parse correctly. D’oh! :slight_smile:

My Audio Unit component still fails to open, saying the dylib is no found. I copied the dylib to “$TARGET_BUILD_DIR$PRODUCT_NAME$WRAPPER_SUFFIX/Contents/Frameworks/libtorch.dylib” (after creating the Frameworks folder). I also used that path instead of “@loader_path/…/Frameworks”, after having some problem using @loader_path that I don’t recall now. My path works for copying. Is it not also correct for install_name_tool?

Or, is Contents/Frameworks not the correct location?

Check if your dylib original path is the one you have used in libtool

otool -L pathToYourExeBinaryNotTheBundle

It says:
@rpath/libtorch.dylib (compatibility version 0.0.0, current version 0.0.0)

I have no idea what @rpath actually means. This isn’t anything I’ve ever had to deal with before. When using dylibs, we’ve always only used ones that were installed by a third party. I tried copying the dylib to /usr/local/lib, thinking that would be a valid “system” location. But that didn’t work, either. I still get the error that @rpath/libtorch.dylib wasn’t found.

@rpath is the “Runpath search path”, there’s a relatively short explanation here that’s probably worth reading. When running otool -L <your_binary>, look for the LC_RPATH command and you’ll see the path it points to. There may be more than one.

There is no LC_RPATH in the listing from that. Only paths to each .framework and each .dylib:

    @rpath/libtorch.dylib (compatibility version 0.0.0, current version 0.0.0)
@rpath/libc10.dylib (compatibility version 0.0.0, current version 0.0.0)
@rpath/libcaffe2_observers.dylib (compatibility version 0.0.0, current version 0.0.0)
@rpath/libshm.dylib (compatibility version 0.0.0, current version 0.0.0)
@rpath/libtorch_global_deps.dylib (compatibility version 0.0.0, current version 0.0.0)
@rpath/libtorch_cpu.dylib (compatibility version 0.0.0, current version 0.0.0)
/System/Library/Frameworks/AudioUnit.framework/Versions/A/AudioUnit (compatibility version 1.0.0, current version 1.0.0)
/System/Library/Frameworks/Accelerate.framework/Versions/A/Accelerate (compatibility version 1.0.0, current version 4.0.0)
/System/Library/Frameworks/AudioToolbox.framework/Versions/A/AudioToolbox (compatibility version 1.0.0, current version 1000.0.0)
/System/Library/Frameworks/AVFoundation.framework/Versions/A/AVFoundation (compatibility version 1.0.0, current version 2.0.0)
/System/Library/Frameworks/AVKit.framework/Versions/A/AVKit (compatibility version 1.0.0, current version 1.0.0)
/System/Library/Frameworks/Carbon.framework/Versions/A/Carbon (compatibility version 2.0.0, current version 162.0.0)
/System/Library/Frameworks/Cocoa.framework/Versions/A/Cocoa (compatibility version 1.0.0, current version 23.0.0)
/System/Library/Frameworks/CoreAudio.framework/Versions/A/CoreAudio (compatibility version 1.0.0, current version 1.0.0)
/System/Library/Frameworks/CoreAudioKit.framework/Versions/A/CoreAudioKit (compatibility version 1.0.0, current version 1.0.0)
/System/Library/Frameworks/CoreMedia.framework/Versions/A/CoreMedia (compatibility version 1.0.0, current version 1.0.0)
/System/Library/Frameworks/CoreMIDI.framework/Versions/A/CoreMIDI (compatibility version 1.0.0, current version 69.0.0)
/System/Library/Frameworks/DiscRecording.framework/Versions/A/DiscRecording (compatibility version 1.0.0, current version 1.0.0)
/System/Library/Frameworks/IOKit.framework/Versions/A/IOKit (compatibility version 1.0.0, current version 275.0.0)
/System/Library/Frameworks/OpenGL.framework/Versions/A/OpenGL (compatibility version 1.0.0, current version 1.0.0)
/System/Library/Frameworks/QuartzCore.framework/Versions/A/QuartzCore (compatibility version 1.2.0, current version 1.11.0)
/System/Library/Frameworks/WebKit.framework/Versions/A/WebKit (compatibility version 1.0.0, current version 609.1.20)
/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 902.1.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1281.100.1)
/System/Library/Frameworks/AppKit.framework/Versions/C/AppKit (compatibility version 45.0.0, current version 1894.40.150)
/System/Library/Frameworks/ApplicationServices.framework/Versions/A/ApplicationServices (compatibility version 1.0.0, current version 52.0.0)
/System/Library/Frameworks/CFNetwork.framework/Versions/A/CFNetwork (compatibility version 1.0.0, current version 0.0.0)
/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 1675.129.0)
/System/Library/Frameworks/CoreGraphics.framework/Versions/A/CoreGraphics (compatibility version 64.0.0, current version 1355.13.0)
/System/Library/Frameworks/CoreServices.framework/Versions/A/CoreServices (compatibility version 1.0.0, current version 1069.22.0)
/System/Library/Frameworks/CoreText.framework/Versions/A/CoreText (compatibility version 1.0.0, current version 1.0.0)
/System/Library/Frameworks/CoreVideo.framework/Versions/A/CoreVideo (compatibility version 1.2.0, current version 1.5.0)
/System/Library/Frameworks/Foundation.framework/Versions/C/Foundation (compatibility version 300.0.0, current version 1675.129.0)
/System/Library/Frameworks/ImageIO.framework/Versions/A/ImageIO (compatibility version 1.0.0, current version 1.0.0)
/usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)

Have you tried adding the path to the “External Library Search Paths” for each configuration, rather than passing the linker flags directly?

install_name_tool -change @rpath/libtorch.dylib @loader_path/../Frameworks/libtorch.dylib "$TARGET_BUILD_DIR/$PRODUCT_NAME$WRAPPER_SUFFIX/Contents/MacOS/$PRODUCT_NAME"

checking your otool result, you will need to do the same for all the @rpath stuff

Otherwise you can maybe add to your linker flags (not using this solution though. not sure what is executable path in a plugin)

-rpath @executable_path/../Frameworks