Symbol not found: _mexPrintf when using mex executable

6 ビュー (過去 30 日間)
Robert
Robert 2024 年 4 月 17 日
編集済み: Abhas 2024 年 12 月 4 日
Hi, I am using a Sonoma 14.3.1 macOs with Apple M1 Pro. I managed to compile a c++ function using mex (It involves libraries such as boost and openMP which I struggled to properly link but ultimately succeedded), but when I run the mex executable in Matlab it crashes. Following the advice in https://es.mathworks.com/matlabcentral/answers/212716-how-do-i-troubleshoot-a-matlab-crash-associated-with-a-custom-mex-function I used 'feval' and a mexhost to debug and try to see what is going on without matlab crashing on me all the time. Here is the code snippet of the function call using feval:
mh = mexhost;
[preds, probs]=feval(mh,'generate_path_structure',A,tols);
where "A" is a sparse matrix with 1s and "tols" is a vector of doubles. Running the matlab code using feval displays the following error message:
Invalid C++ MEX-file 'generate_path_structure': dlopen(/Users/robertbenassai/Documents/UOC/MCM/mcm/generate_path_structure.mexmaca64, 0x0006):
Symbol not found: _mexPrintf
Referenced from: <C58C4F04-7EBD-313D-A9BD-61548F72625D> /Users/robertbenassai/Documents/UOC/MCM/mcm/generate_path_structure.mexmaca64
Expected in: <A1867563-F598-3005-B8E0-8FFDD5D68CA6> /Applications/MATLAB_R2023b.app/interprocess/bin/maca64/mex/libmex.dylib
Which I think happens because Matlab doesn't find the libmex.dylib library. Before having a "Symbol not found" with 'mexPrintf' I had the same issue with 'mexErrMsgIdAndTxt' but I was able to comment the lines where it appeared, which I can't do with mexPrintf because it is probably included in another mex function I am using.
I have tried changing the DYLD_LIBRARY_PATH variable to add the path /Applications/MATLAB_R2023b.app/interprocess/bin/maca64/mex/libmex.dylib but I haven't succeeded.
My compilation line is:
mex -largeArrayDims generate_path_structure.cpp -I"/opt/homebrew/include" -I"/Applications/MATLAB_R2023b.app/extern/include" -L"/opt/homebrew/lib" -L"/Applications/MATLAB_R2023b.app/bin/maca64" -lboost_system -lboost_filesystem -lboost_graph CXXFLAGS="\$CXXFLAGS -std=c++20 -fopenmp" LDFLAGS="\$LDFLAGS -Xpreprocessor -fopenmp"
And compiles just fine. I've also tried to compile linking /Applications/MATLAB_R2023b.app/interprocess/bin/maca64/mex but it gives the following error on compilation related to mexPrintf:
Error using mex
Undefined symbols for architecture arm64:
"_mexPrintf", referenced from:
_mexFunction in generate_path_structure.o
_mexFunction.omp_outlined in generate_path_structure.o
ld: symbol(s) not found for architecture arm64
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
I am quite new to Matlab and I would really appreciate if someone could help me find the reason of the issue. I am also not a computer scientist so I'm sorry for being messy and incorrect in my question, my knowledge is pretty limited...
Thank you beforehand

採用された回答

Abhas
Abhas 2024 年 12 月 4 日
編集済み: Abhas 2024 年 12 月 4 日
The issue you are facing is primarily due to the following:
  • Thread Safety Issue: "mexPrintf" and similar functions (e.g., stream out, memory allocation) are not thread-safe and cannot be used inside "#pragma omp parallel" or other parallel structures in OpenMP. This can cause crashes during execution. You may refer to the below MATLAB Answers link to know more about thread safety of mex-functions: https://www.mathworks.com/matlabcentral/answers/418782-thread-safety-of-mx-mex-functions
  • Improper Usage: Using "mexPrintf" inside a parallel block (#pragma omp parallel) results in undefined behavior, as shown in your code.
To fix the issue you can follow the below steps:
  • Move "mexPrintf" and other non-thread-safe operations outside parallel constructs. Use "OpenMP" only for computationally intensive sections. Here's an example code:
#include "mex.h"
#include "omp.h"
void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) {
double sum = 0.0;
#pragma omp parallel for reduction(+: sum)
for (int p = 0; p < 100000000; ++p) {
sum += (double)(p) * (double)(p);
}
mexPrintf("sum = %f\n", sum); // Output is safe outside parallel block
}
  • Compile using appropriate flags to enable "OpenMP":
mex CXX_FLAGS="-Xclang -fopenmp" LDFLAGS="$LDFLAGS -lomp" CXXOPTIMFLAGS="$CXXOPTIMFLAGS -Xclang -fopenmp" -I/usr/local/include test1.cpp
You may also refer to the below resources that might help you solve your issue:
I hope this helps!

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeWrite C Functions Callable from MATLAB (MEX Files) についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by