DYLD_LIBRARY_PATH from shell not passed to Matlab on Mac

7 views (last 30 days)
I am developing an interface to a shared library, using the matlab clib utilities (https://mathworks.com/help/matlab/ref/clibgen.generatelibrarydefinition.html etc.). Since that library has some dependencies, those need to be found as well when running the interface.
The interface needs to work on all of Windows, Linux and Mac. The documentation has instructions on what needs to be done (https://mathworks.com/help/matlab/matlab_external/set-run-time-library-path-for-c-interface.html) for finding dependencies. For Windows and Linux, these instructions work correctly, and my interface library runs. However, for Mac, the instructions do not work.
What I do: in a bash shell on the Mac, I export the updated DYLD_LIBRARY_PATH. Then from that shell, I start Matlab. However, inside Matlab, the DYLD_LIBRARY_PATH environment variable shows as empty, whereas other environment variables do get through. Thus, when I try to run functions from my interface library, it fails to load because of missing dependencies.
To reproduce:
in a bash shell, do both
export DYLD_LIBRARY_PATH=to_test_dyld
export MY_OWN_VAR=to_test_other
then start matlab from that shell, and inside matlab do both
getenv('DYLD_LIBRARY_PATH')
getenv('MY_OWN_VAR')
The first will give an empty char array, whereas the second will correctly display 'to_test_other'.
How can I make it so the DYLD_LIBRARY_PATH is passed to Matlab (and beyond, since I run the interface library outofprocess)?

Accepted Answer

Aljen Uitbeijerse
Aljen Uitbeijerse on 5 Jun 2023
Edited: Aljen Uitbeijerse on 8 Jun 2023
The correct answer, modified from the support answer.
(Note that this is correct w.r.t. "How can I make it so the DYLD_LIBRARY_PATH is passed to Matlab on Mac", for me it still isn't the full answer, because the library still fails to run through the matlab clib)
This happens because of System Integrity Protection on Mac computers running macOS version El Capitan (10.11) or newer. This security feature strips the "DYLD_LIBRARY_PATH" environment variable when launching protected executables. To work around this, instead of an “export” command, set the variable as part of the same line where the command is:
DYLD_LIBRARY_PATH="<path here>" <your command here or full path to executable>
For example, to launch MATLAB and pass in a custom "DYLD_LIBRARY_PATH" (the full path to your MATLAB executable could be different depending on your install location), you could run:
DYLD_LIBRARY_PATH="<path here>" /Applications/MATLAB_R2023a.app/Contents/MacOS/MATLAB
Please note that it's the path to the actual UNIX executable and not the ".app" file.
To make it more in line with the example in the matlab documentation, and to do have the export command still there (so the variable is available in other shells that do not launch protected executables), and because <path here> may actually be a very long line, you'd put this in two lines:
export DYLD_LIBRARY_PATH="<path here>"
DYLD_LIBRARY_PATH="${DYLD_LIBRARY_PATH}" /Applications/MATLAB_R2023a.app/Contents/MacOS/MATLAB
  1 Comment
Nathan
Nathan on 9 Jan 2024
Tried this and still found DYLD_LIBRARY_PATH unset after launch. Please let me know if there are any other suggestions you might have from your experience.

Sign in to comment.

More Answers (1)

Aljen Uitbeijerse
Aljen Uitbeijerse on 5 Jun 2023
Here the, almost correct, answer that I received on my support ticket. It did set me on the way to the correct answer. I'll put that in a separate answer, to clearly distinguish what should or should not be done.
(If there is a MATLAB answers policy on how to handle this situation correctly, with answers received elsewhere, maybe a staff member can update this thread?)
From my understanding, you are attempting to set the "DYLD_LIBRARY_PATH" environment variable on macOS but this variable isn’t being passed into MATLAB when launching from the terminal (but other custom variables are). I would be happy to look into this issue.
This happens because of System Integrity Protection on Mac computers running macOS version El Capitan (10.11) or newer. This security feature strips the "DYLD_LIBRARY_PATH" environment variable when launching protected executables. To work around this, prepend the “export” command right at the beginning of the command you run like so:
export DYLD_LIBRARY_PATH="<path here>" <your command here or full path to executable>
For example, to launch MATLAB and pass in a custom "DYLD_LIBRARY_PATH" (the full path to your MATLAB executable could be different depending on your install location), you could run:
export DYLD_LIBRARY_PATH="<path here>" /Applications/MATLAB_R2023a.app/Contents/MacOS/MATLAB
Please note that it's the path to the actual UNIX executable and not the ".app" file.

Categories

Find more on Install Products in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!