Best way to locate shared (.dylib) libraries used by MATLAB mex files on MacOS X
22 views (last 30 days)
I'm using MATLAB on a MacOS X system, and building mex files. These mex files depend on various shared (.dylib) libraries, which I keep in the same directory as the mex files.
On older versions of MacOS X, in order to find those shared libraries at MATLAB run time, it was sufficient to have the environment variable DYLD_LIBRARY_PATH set to include that directory name. The variable could be set before starting MATLAB, or by setting it in a copy of the matlab script. Either way, so long as DYLD_LIBRARY_PATH was set to include the location before starting MATLAB or by making a copy of the matlab script which included a line to set DYLD_LIBRARY_PATH correctly, the mex files would work properly. If the environment variable was not set correctly, MATLAB would just issue an error message saying that dylibs couldn't be found.
But, on recent versions of MacOS X (or, perhaps, it's recent MacOS X combined with recent MATLAB - I'm not sure) this no longer works - DYLD_LIBRARY_PATH gets ignored, and the mex files fail, I guess because of Apple's tightened security policies.
What does work is to make sure that MATLAB's current working directory is the directory containing the mex files and dylibs when using the mex files for the first time. After that you can change directories and the mex files still work. When you do this there's no need to worry about DYLD_LIBRARY_PATH. Presumably once the dylibs have been loaded in this way, they stay loaded and no longer need to be in your path. As far as I can tell this works with all versions of MATLAB from at least R2015b up to R2017b and with all versions of MacOS up to the latest I've got access to, which is MacOS X 10.12 (sierra).
So - when I distribute my mex files to other people, I just need to tell them that they need to be in the right directory before they first use one of them (or add code to their startup.m file to do it for them). In fact, I've not found any other way of getting it to work.
My question is - is this a sensible way of ensuring that dylibs needed by mex files on MacOS are located? If not, what is the recommended way?
Jim Hokanson on 7 Mar 2018
I've had some luck with the following command in the same directory as my mex file.
install_name_tool -change /Users/jim/Documents/repos/matlab_git/mex_lib_code/zip/libzip.dylib @loader_path/libzip.dylib kzip_mex.mexmaci64
or more generally:
install_name_tool -change <old_dylib_path> @loader_path/<dylib_file_name> <mex_file_name>
To get the first value, run:
otool -L kzip_mex.mexmaci64
This should report where the dylibs are being loaded from. Use these values as the first input.
The second input, in my case @loader_path/libzip.dylib, should be replaced with your dylib name, keeping the @loader_path part.
Finally, the third input specifies what file should be targeted for this change. Give it the name of your mex file.
Rerun the otool command and now you should see the path to the dylib has changed ...