Issue: Library not loaded error

It seems that we are getting error messages when running compiled Mex files that the dynamic link libraries cannot be resolved at runtime. The error looks like this:

>> mglResolution                                    
Invalid MEX-file '/Users/justin/proj/mgl/mgllib/mglResolution.mexmaci64': dlopen(/Users/justin/proj/mgl/mgllib/mglResolution.mexmaci64,
0x0006): Library not loaded: @rpath/eyelink_core.framework/Versions/A/eyelink_core
  Referenced from: <C8BD6C36-CF02-3A42-94F4-F86C0D758CF3> /Users/justin/proj/mgl/mgllib/mglResolution.mexmaci64
  Reason: tried: '/Applications/MATLAB_R2022b.app/bin/maci64/eyelink_core.framework/Versions/A/eyelink_core' (no such file),
  '/Applications/MATLAB_R2022b.app/bin/maci64/./eyelink_core.framework/Versions/A/eyelink_core' (no such file),
  '/Applications/MATLAB_R2022b.app/bin/maci64/../../sys/os/maci64/eyelink_core.framework/Versions/A/eyelink_core' (no such file),
  '/Applications/MATLAB_R2022b.app/bin/maci64/eyelink_core.framework/Versions/A/eyelink_core' (no such file),
  '/Applications/MATLAB_R2022b.app/bin/maci64/./eyelink_core.framework/Versions/A/eyelink_core' (no such file),
  '/Applications/MATLAB_R2022b.app/bin/maci64/../../sys/os/maci64/eyelink_core.framework/Versions/A/eyelink_core' (no such file),
  '/Applications/MATLAB_R2022b.app/bin/maci64/../../runtime/maci64/eyelink_core.framework/Versions/A/eyelink_core' (no such file),
  '/Applications/MATLAB_R2022b.app/bin/maci64/../../sys/java/jre/maci64/jre/lib/server/eyelink_core.framework/Versions/A/eyelink_core' (no
  such file), '/Applications/MATLAB_R2022b.app/bin/maci64/../../sys/java/jre/maci64/jre/lib/eyelink_core.framework/Versions/A/eyelink_core'
  (no such file)

This is weird because at compile time a linker flag -F/Library/Frameworks should be telling dyld to go look for the framework in that directory (where it exists), but something is failing. This maybe some Mathworks bug, so would hope that this gets resolved by an update.

Fix

In the meantime, one fix is to explicitly set the location of the frameworks that are not dynamically linking in the binary.

First, you can see all the paths for all the dynamic link libraries needed in the binary, using otool

> otool -L mglResolution.mexmaci64 
mglResolution.mexmaci64:
	/System/Library/Frameworks/Carbon.framework/Versions/A/Carbon (compatibility version 2.0.0, current version 170.0.0)
	/System/Library/Frameworks/Cocoa.framework/Versions/A/Cocoa (compatibility version 1.0.0, current version 24.0.0)
	/System/Library/Frameworks/CoreServices.framework/Versions/A/CoreServices (compatibility version 1.0.0, current version 1226.0.0)
	/System/Library/Frameworks/OpenGL.framework/Versions/A/OpenGL (compatibility version 1.0.0, current version 1.0.0)
	/System/Library/Frameworks/QTKit.framework/Versions/A/QTKit (compatibility version 1.0.0, current version 1.0.0)
	/System/Library/Frameworks/CoreAudio.framework/Versions/A/CoreAudio (compatibility version 1.0.0, current version 1.0.0)
	@rpath/eyelink_core.framework/Versions/A/eyelink_core (compatibility version 1.0.0, current version 1.0.0)
	@rpath/edfapi.framework/Versions/A/edfapi (compatibility version 1.0.0, current version 1.0.0)
	@rpath/libmx.dylib (compatibility version 0.0.0, current version 0.0.0)
	@rpath/libmex.dylib (compatibility version 0.0.0, current version 0.0.0)
	@rpath/libmat.dylib (compatibility version 0.0.0, current version 0.0.0)
	/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 1600.151.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1336.0.0)
	/System/Library/Frameworks/AppKit.framework/Versions/C/AppKit (compatibility version 45.0.0, current version 2483.0.0)
	/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 2048.1.255)
	/System/Library/Frameworks/CoreGraphics.framework/Versions/A/CoreGraphics (compatibility version 64.0.0, current version 1774.0.4)
	/System/Library/Frameworks/Foundation.framework/Versions/C/Foundation (compatibility version 300.0.0, current version 2048.1.255)
	/usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)

You can then use install_name_tool to fix the @rpath libraries to have a fixed location:

> install_name_tool -change @rpath/eyelink_core.framework/Versions/A/eyelink_core /Library/Frameworks/eyelink_core.framework/Versions/A/eyelink_core mglResolution.mexmaci64 
> install_name_tool -change @rpath/edfapi.framework/Versions/A/edfapi /Library/Frameworks/edfapi.framework/Versions/A/edfapi mglResolution.mexmaci64 

Likely you only need to do this for the first function that gets called by Matlab for which it can't resolve the library. Once a function runs there seems to be some caching that is happening because other functions that don't have the fixed library paths will now all run properly.