MATLAB:mex​:ErrInvali​dMEXFile : Invalid MEX-file: The specified procedure could not be found.

I have been facing a vicious Mex runtime error for weeks now that is significantly wasting my time without getting anywhere to resolve it. The error message is in the title of this question. Surprisingly, the compiled Mex files run smoothly under MATLAB 2023b and 2024a, but not under the older versions of MATLAB. Even stranger, I am compiling the Mex file with MATLAB 2022a, and removing all other matlab installations from the paths, and still getting this runtime error with the same MATLAB 2022a, but not with other newer installations.
What could be going wrong? How can I trace the root of this persistent error that sometimes changes to slightly different messages?

10 Comments

First use
which
command and make sure MATLAB (2022a) looks for the right mex file.
Hi Bruno thanks for your suggestion. This cannot be the problem, because I am using CMake and Windows batch to compile the Mex files and I have meticulously checked all output messages to ensure only the specific targeted MATLAB libraries and binaries are used. I even eliminated all other MATLAB versions from the path before compilations. Still the code appears to works with MATLAB 2024 and 2023 but not with older version, even the code that was compiled with older versions' libraries.
At this point, I need a runtime debugger for the Mex to find out where the problems happen. If you or anyone has any ideas on this, I would appreciate it.
I'm affraid the debugger won't help since it cannot even load the mex file.
I would sugget to test with another compiler if you can. What compiler are you using? Do you compile with -v option to see if the verbose can help to detect what could go wrong?
I have Intel ifort / icx. I can give it a try with GNU compilers to see if it works.
Actually, I have tried with GNU compilers and it seems to work, but there seem to be other issues (STDOUT piping) that make the GNU option less interesting.
If you use more recent version that might be an issue.
That could be it (Version 2021.11.1 Build 20231117_000000), but I doubt it. I will try an older version too just to rule out possibilities.
Just wonder how you could even compile the MEX in R2022a, the (non supported) compiler shouldn't be even detected.
Do you compile under more recent MATLAB and copy the mex file?
Her is the output from mex -setup
MEX configured to use 'Microsoft Visual C++ 2022 (C)' for C language compilation.
Is microsoft CL.exe supported?
"MEX configured to use 'Microsoft Visual C++ 2022 (C)' for C language compilation.=Is microsoft CL.exe supported?"
According to the official link I post above, yes. Also if the compiler can be detected by
mex -setup
then it is supported.

Sign in to comment.

Answers (1)

Hello @A.B.,
According to the documentation:
For best results, your version of MATLAB must be the same version that was used to create the MEX file.
MEX files use MATLAB run-time libraries. A MEX file that was created on an earlier version of MATLAB usually runs on later versions of MATLAB. If the MEX file generates errors, recompile the MEX file from the source code.
Now, addressing the issue:
"Even stranger, I am compiling the Mex file with MATLAB 2022a, and removing all other matlab installations from the paths, and still getting this runtime error with the same MATLAB 2022a, but not with other newer installations."
Based on my understanding, I have identified a workaround that might help to find the cause of the issue.
The error message highlighted by you occures in the case when MATLAB cannot find all .DLL files referenced by a MEX file.
Generally, it throws an error as shown below:
Invalid MEX-file mexfilename:
The specified module could not be found.
where mexfilename is the module with the dependency error. This module cannot find its dependent libraries. To resolve this error, find the names of the dependent libraries, and determine if they are present on your system and on the system path.
For windows operating system, you can use “Dependency Walker Utility” for this purpose
Dependency Walker is a tool that tracks and logs files that are accessed when an executable launches. It can also track what files are being called by a DLL or SYS file. It is able to track file registration errors, access violations, invalid page faults or even missing files. It can be downloaded from the website:
To know more about “Dependency walker utility”, refer:
you can also refer to the below MATLAB answer, which addresses somewhat similar issue:
I hope it helps!

4 Comments

What I don't understand in your suspition is the missing DLL - if any - is obviously found with more recent LATLAB Mex, so it should be there somewhere in the path.
what I understand from the question is, the compiled MEX file is running with more recent version of MATLAB, but while compiling it using MATLAB R2022a, it is giving error. That's why I suspected that the appropriate DLL file might have been missing or corrupted or not properly located on the path while trying out MATLAB R2022a.
Then the issue is the compiled mex is not backward compatible, not missing DLL in my view.
Thank you for your responses. I could not fix the problem. The suspicions raised here about compiler compatibility with MEX may be right because the runtime errors only happen with MATLAB R2022b and older, but the mex file works perfectly fine with newer version even on other computers.
However, what is truly bizarre, is that I have an older version of the this MEX file (which calls another DLL just like the newer version). This old MEX file compiles and runs perfectly fine with all MATLAB versions, depite having identical MEX build commands and flags and MATLAB MEX version. I even removed the additional newly included libraries in the new MEX source file, trying to make it as similar as possible to the old MEX file. I compared the build commands word by word and all are the same. Yet, the new MEX file does not run successfully on older MATLAB versions. How could this be possible? The only difference in the build process is for the DLL which both MEX files call. The nex MEX file calls a DLL that has been built with CMake, wehereas the old MEX file calls a DLL that has been built manually by calling the same Fortran compiler that CMake uses on the command line in a windows batch file.
The dependency walker suggested above identifies five errors in the new MEX compiled file. I could not figure out where these errors are, as the software does not offer any readily digestible clues. But regardless, how could these errors cause problems only for the older MATLAB versions?

Sign in to comment.

Categories

Products

Asked:

on 4 Oct 2024

Edited:

on 25 Oct 2024

Community Treasure Hunt

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

Start Hunting!