maci64/libMatlabEngine.dylib not found: trying to call a compiled .mexmaci64 function on macOS
조회 수: 50 (최근 30일)
이전 댓글 표시
I use a macOS and I want to compile a cpp function so that it becomes callable from matlab. The code for it:
#include "matrix.h" // MATLAB matrix header file
#include "mex.h" // MATLAB MEX header file
void mycppfunction(double* input, double* output, int size) {
// Your C++ code here
for (int i = 0; i < size; ++i) {
output[i] = 2 * input[i];
}
}
void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) {
// Check input arguments
if (nrhs != 1) {
mexErrMsgIdAndTxt("MyCppFunction:InvalidInput", "One input argument required.");
}
// Get input data
double* input = mxGetPr(prhs[0]);
int size = mxGetNumberOfElements(prhs[0]);
// Create output array
plhs[0] = mxCreateDoubleMatrix(1, size, mxREAL);
double* output = mxGetPr(plhs[0]);
// Call C++ function
mycppfunction(input, output, size);
}
I have installed Xcode 15 and compiled it function (the file is called test.cpp) using:
mex test.cpp
and the matlab terminal outputs:
Building with 'Xcode Clang++'.
ld: warning: -undefined error is deprecated
ld: warning: -undefined error is deprecated
ld: warning: -undefined error is deprecated
ld: warning: -undefined error is deprecated
MEX completed successfully.
Now I attempt to run the compiled function by:
result = test([1,2,3]);
But I get the error:
Invalid MEX-file '/Users/usrname/Desktop/workspace/pjct/test.mexmaci64': dlopen(/Users/usrname/Desktop/workspace/pjct/test.mexmaci64, 0x0006): Library
not loaded: @rpath/libMatlabEngine.dylib
Referenced from: <1E2FE79D-9801-39D7-860B-61EE635A1D7A> /Users/usrname/Desktop/workspace/pjct/test.mexmaci64
Reason: tried: '/Applications/MATLAB_R2023b.app/bin/maci64/libMatlabEngine.dylib' (no such file),
'/Applications/MATLAB_R2023b.app/bin/maci64/./libMatlabEngine.dylib' (no such file),
'/Applications/MATLAB_R2023b.app/bin/maci64/../../sys/os/maci64/libMatlabEngine.dylib' (no such file),
'/Applications/MATLAB_R2023b.app/Contents/MacOS/libMatlabEngine.dylib' (no such file),
'/Applications/MATLAB_R2023b.app/Contents/MacOS/./libMatlabEngine.dylib' (no such file),
'/Applications/MATLAB_R2023b.app/Contents/MacOS/../../standalone/bin/maci64/libMatlabEngine.dylib' (no such file),
'/Applications/MATLAB_R2023b.app/Contents/MacOS/../../sys/os/maci64/libMatlabEngine.dylib' (no such file),
'/Applications/MATLAB_R2023b.app/bin/maci64/libMatlabEngine.dylib' (no such file)
I have been searching online for solutions but no productive results.
Any help is greatly appreciated.
댓글 수: 0
답변 (2개)
Jinal
2023년 10월 23일
Hello Shuwei,
As per my understanding, you are encountering errors while executing a C++ MEX function.
You can try the following workaround to resolve the issue at hand:
Set the "DYLD_LIBRARY_PATH" environment variable to the path specified below:
‘matlabroot/extern/bin/maci64’
For more information on Run-Time Environment requirements for building C++ Engine programs, please refer the following link: https://www.mathworks.com/help/matlab/matlab_external/build-c-engine-programs.html#mw_4ec5bd7e-1127-41a8-844c-817cb7a9cae3
Additionally, you may also refer the following documentation page to know more about 'Invalid MEX File Errors': https://www.mathworks.com/help/matlab/matlab_external/invalid-mex-file-error.html
I hope that the suggested workaround helps resolve the error you are currently experiencing.
댓글 수: 2
Ciara Torguson
2024년 2월 4일
편집: Ciara Torguson
2024년 2월 8일
Hello!
I'm getting the same error with a MEX file (or C++ application using MATLAB Engine API) where it fails to find libMatlabEngine.dylibat runtime, resulting in the following error message:
"Invalid MEX-file: Library not loaded: @rpath/libMatlabEngine.dylib".
Troubleshooting Steps Already Taken:
- Verified that libMatlabEngine.dylib exists in /Applications/MATLAB_R2023b.app/extern/bin/maci64.
- Modified the DYLD_LIBRARY_PATH environment variable to include the path to MATLAB's bin/maci64 directory.
- Used install_name_tool to add an rpath pointing to /Applications/MATLAB_R2023b.app/extern/bin/maci64 directly in the MEX file.
- Confirmed the existence and correct path setting of DYLD_LIBRARY_PATH in the terminal and MATLAB environment.
- Recompiled the MEX file with MATLAB's mex command, ensuring all include directories and library paths for CGAL, Eigen, and MATLAB Engine API were correctly specified.
- Checked for potential conflicts or issues in MATLAB's project path and environment settings.
Despite these steps, the issue persists. I'm looking for insights or additional troubleshooting steps that might resolve this error. Suggestions on ensuring MATLAB's runtime libraries are correctly found by MEX files or C++ applications using MATLAB Engine API on macOS would be greatly appreciated.
Environment:
- Operating System: macOS M2
- MATLAB Version: R2023b
- MATLAB Runtime Version: (if applicable, specify version)
- CGAL Version: 5.6
- Compiler: (e.g., Clang 12.0.0, specify if using Xcode or another toolchain)
- Architecture: x86_64
Additional Notes:
- I'm using CMake for the build configuration, ensuring the MATLAB and CGAL paths are correctly included.
- The application needs to access MATLAB functions and CGAL for computational geometry processing.
- I'm open to trying alternative methods or configurations if they can help resolve this issue.
Thank you in advance for any help or suggestions you can provide!
Ciara Torguson
2024년 2월 8일
I finally resolved the error, here are the steps I took;
1. Editing the Shell Profile
Depending on the shell you are using (bash or zsh), you will edit a different file. macOS Catalina and later versions use zsh as the default shell, while earlier versions used bash.
- For bash (macOS Mojave and earlier by default): Edit ~/.bash_profile or ~/.bashrc
- For zsh (macOS Catalina and later by default): Edit ~/.zshrc
2. Adding the DYLD_LIBRARY_PATH Variable
- Open Terminal.
- Open the profile file for editing:
- For bash:sh
- open -e ~/.bash_profile
* If ~/.bash_profile does not exist, try ~/.bashrc.
- For zsh:sh
- open -e ~/.zshrc
- Add the export command to the file:sh
- export DYLD_LIBRARY_PATH=/Applications/MATLAB_R2023b.app/bin/maci64:/Applications/MATLAB_R2023b.app/extern/bin/maci64:$DYLD_LIBRARY_PATH
*Insert your own path and Save and close the editor.
3. Making the Changes Take Effect
- For the changes to take effect in the current terminal session, source the profile file:
- For bash:sh
source ~/.bash_profile
If you edited ~/.bashrc instead, use:
sh
- source ~/.bashrc
- For zsh:sh
- source ~/.zshrc
- To ensure the changes persist in MATLAB, especially if you're launching MATLAB from its GUI and not from the terminal, you might need to set these environment variables within MATLAB itself or ensure MATLAB is always launched from a terminal session where these variables are set.
4. Launching MATLAB from Terminal (Optional)
To ensure MATLAB uses these settings, you can create an alias in your shell profile file (~/.bash_profile or ~/.zshrc) that starts MATLAB, ensuring it inherits the terminal's environment variables:
sh
alias matlab='/Applications/MATLAB_R2023b.app/bin/matlab &'
After adding this, you'll need to source your profile file again as shown above.
5. Verifying the Setup
- Restart your terminal or open a new one.
- Echo the DYLD_LIBRARY_PATH to verify it's set:sh
- echo $DYLD_LIBRARY_PATH
- Launch MATLAB from this terminal (if using the alias, just type matlab) and verify that your MEX files work without the previously encountered runtime error.
Note:
Be cautious when setting DYLD_LIBRARY_PATH globally, as it can affect how other applications resolve their dynamic libraries. If you encounter issues with other applications, you might consider setting this variable only in specific circumstances or scripts where you need it.
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!