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?
4 Comments
Direct link to this comment
https://au.mathworks.com/matlabcentral/answers/359497-best-way-to-locate-shared-dylib-libraries-used-by-matlab-mex-files-on-macos-x#comment_489824
Direct link to this comment
https://au.mathworks.com/matlabcentral/answers/359497-best-way-to-locate-shared-dylib-libraries-used-by-matlab-mex-files-on-macos-x#comment_489824
Direct link to this comment
https://au.mathworks.com/matlabcentral/answers/359497-best-way-to-locate-shared-dylib-libraries-used-by-matlab-mex-files-on-macos-x#comment_489904
Direct link to this comment
https://au.mathworks.com/matlabcentral/answers/359497-best-way-to-locate-shared-dylib-libraries-used-by-matlab-mex-files-on-macos-x#comment_489904
Direct link to this comment
https://au.mathworks.com/matlabcentral/answers/359497-best-way-to-locate-shared-dylib-libraries-used-by-matlab-mex-files-on-macos-x#comment_490093
Direct link to this comment
https://au.mathworks.com/matlabcentral/answers/359497-best-way-to-locate-shared-dylib-libraries-used-by-matlab-mex-files-on-macos-x#comment_490093
Direct link to this comment
https://au.mathworks.com/matlabcentral/answers/359497-best-way-to-locate-shared-dylib-libraries-used-by-matlab-mex-files-on-macos-x#comment_491828
Direct link to this comment
https://au.mathworks.com/matlabcentral/answers/359497-best-way-to-locate-shared-dylib-libraries-used-by-matlab-mex-files-on-macos-x#comment_491828
Sign in to comment.