MATLAB: Can anyone help me resolve setting a path to dynamic library in an Invalid MEX-file runtime error

image not foundinvalid mex errormex

Hello, I'm trying to integrate a hardware interface I built in cpp into Matlab using mex (Matlab 2017b, Xcode 9 Clang++, MacOS10.12, ). The .mexmaci64 compiles and links without any problems but despite my specifying -L and -l library paths/dylibs, at runtime I get an invalid mex file error:
Invalid MEX-file '/Users/dnarain/WORKING/DnDriverBasler/GrabIm.mexmaci64': dlopen(/Users/dnarain/WORKING/DnDriverBasler/GrabIm.mexmaci64, 6): Library not loaded: @rpath/pylon.framework/Versions/A/Libraries/libpylonbase-5.0.5.dylib Referenced from: /Users/dnarain/WORKING/DnDriverBasler/GrabIm.mexmaci64 Reason: image not found.
My attempted solutions: I assumed the library path was incorrect and tried to set the variable LD_LIBRARY_PATH manually in Matlab. It didn't resolve the runtime error. I even tried modifying the RPATH -rpath-link line in mexopts.sh: from : RPATH="-Wl,-rpath-link,$TMW_ROOT/bin/$Arch" to: RPATH="-Wl,-rpath-link,/Library/Frameworks:$TMW_ROOT/bin/$Arch,-rpath=/Library/Frameworks:$TMW_ROOT/bin/$Arch"
However, no luck at runtime. Would greatly appreciate if anyone might have another solution to this.
Thanks, Dev
Other information: This is what I get when I check otool -L: /System/Library/Frameworks/Cocoa.framework/Versions/A/Cocoa (compatibility version 1.0.0, current version 22.0.0) @rpath/pylon.framework/Versions/A/Libraries/libpylonbase-5.0.5.dylib (compatibility version 5.0.0, current version 5.0.5) @rpath/pylon.framework/Versions/A/Libraries/libpylonutility-5.0.5.dylib (compatibility version 5.0.0, current version 5.0.5) @rpath/pylon.framework/Versions/A/Libraries/libGCBase_gcc_v3_0_Basler_pylon_v5_0.dylib (compatibility version 0.0.0, current version 0.0.0) @rpath/pylon.framework/Versions/A/Libraries/libGenApi_gcc_v3_0_Basler_pylon_v5_0.dylib (compatibility version 0.0.0, current version 0.0.0) @rpath/libmex.dylib (compatibility version 0.0.0, current version 0.0.0) /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 400.9.0) /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1252.0.0)

Best Answer

I succeeded in passing the -rpath to the linker during the mex run time using
'LDFLAGS=$LDFLAGS -Wl -rpath /Library/Frameworks/', ...
Related Question