Why doesn't simple MEX compilation work on Linux

37 views (last 30 days)
I am using MATLAB R2023a on Arch Linux and I am trying to compile a MEX project. However, I get the error
Error using mex
'/home/xxx/yprime.mexa64' is not a MEX file. For more information, see File is not a MEX file.
I read up on MEX and found that the most recent version of gcc provided by Arch Linux is too recent (13) compared to what MATLAB supports (10). So I installed GCC version 10 from the AUR and tried compiling using mex GCC='/usr/bin/gcc-10 program.c but I still get the same error.
If I try the example codes provided by MATLAB (like yprime.c) I manage to get a working program (even with the error above), but other code I have tried simply leads to programs that yields segmentation faults that crashes MATLAB.
Using the verbose setting doesn't give me any more info on what is erroring, so I am kind of stuck. I also checked the instructions here https://wiki.archlinux.org/title/MATLAB#Install_supported_compilers to set the compiler to the old version, but from the output it seems like MATLAB is still using the system-wide installation of the compiler, so I guess that info is a bit outdated.
If anyone has any guidance on what to do next I'd love to hear some suggestions.
  3 Comments
Lucas Hedström
Lucas Hedström on 15 Sep 2023
Thanks for the quick response. The output of that commend is simply
>> mex -setup
MEX configured to use 'gcc' for C language compilation.
To choose a different language, select one from the following:
mex -setup C++
mex -setup FORTRAN
Nothing else shows up. If I type mex -setup c I get
>> mex -setup c
MEX configured to use 'gcc' for C language compilation.
Lucas Hedström
Lucas Hedström on 18 Sep 2023
mex -v GCC='/usr/bin/gcc-10' timestwo.c
Which gives the same error. However, the compiler uses the correct GCC version, or that is what the output is telling me anyway.

Sign in to comment.

Answers (1)

Narvik
Narvik on 25 Oct 2023
Hi,
I understand that you are facing an issue while trying to compile a MEX project on Linux. The error “...mexa64 is not a MEX file” may be caused by a mismatch between the runtime libraries shipped for GCC with your system OS and MATLAB.
To solve this issue, you can direct MATLAB to use the operating system’s runtime libraries instead of MATLAB shipped version. This can be done by setting the `LD_PRELOAD` environment variable and restarting MATLAB.
You can run the following sample command on your terminal:
% For libstdc++ library in Ubuntu. The system libstdc++ can be force loaded with the required version.
export LD_PRELOAD=/lib/x86_64-linux-gnu/libstdc++.so.6 matlab
As an alternative, you can allow libraries from your GCC installation to be called and remove libraries from MATLAB by following the instructions in the MATLAB answer below:
Hope this helps!
  2 Comments
Manoj
Manoj on 8 Dec 2023
Hi Narvik,
Thanks for providing this answer. But I feel that almost any user who uses mex functions or mex s-functions will run into this problem. So a cleaner solution is preferable.
I have two specific concerns with your workaround,
  • What if there is a bug in glibc that crashes Simulink and the fix for that is in MathWorks shipped glibc. This happened just two years back.
  • How do I make sure MATLAB always start with LD_PRELOAD set as an end user?
Maybe one better workaround would be to,
  • Make mex command use glibc provided by MATLAB (instead of system one). It may have to pass some flags to gcc.
Lucas Hedström
Lucas Hedström on 14 Dec 2023
Hi,
I finally returned to this issue.
Unfortunately, neither setting LD_PRELOAD nor removing the files as the other solution suggested helped.
I am at a loss.

Sign in to comment.

Categories

Find more on Write C Functions Callable from MATLAB (MEX Files) 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!