mex file only found when in working directory, but not when in search path

I would like to use a toolbox containing .m functions and (pre-compiled) .mex files.
The path containing both the function and the mex file are included in the search path. I verified that Matlab can see the function, as help functionname returns the correct help file.
When I try to run some code calling one of the functions, I get the error:
"Invalid MEX-file 'C:\somepath\mexfilename.mexw64': The specified module could not be found."
Note, that it could find the function, but not the mex file located in the same directory as the function.
If I remove the directory from the search path, it (obviously) cannot find the function either, creating a different error:
"Undefined function 'functionname' for input arguments of type 'char'."
The strange thing is, that when I set the working directory to the location where the function and the mex file are, everything works.
But, as mentioned, if the directory is only in the search path, but not currently the working directory, it can find the function, but not the mex file.
Seeing that the code runs perfectly well if the working directory points at the mex fiel, I would exclude all problems related to compilation and/or running of the mex files. I think the problem is with the path / visibility of the file, but then why does it see the function?
I recently switched computers. On my former computer I already used the same toolbox and did not run into this problem (Matlab 2018b on win10 in both cases).

 Akzeptierte Antwort

Philip Borghesani
Philip Borghesani am 1 Apr. 2019
Bearbeitet: Philip Borghesani am 2 Apr. 2019
Is the mex file dependent on other dlls in the same directory? It sounds to me that Matlab is finding the mex file but not some other dependencies.
If that is the problem you could add the directory to the dos path before starting MATLAB or move the other needed dlls to a directory on the system path when MATLAB is running.

1 Kommentar

There are two dll files in the same directory, since they are supplied by the people who wrote the toolbox, I assume they are needed.
Before installing and running the dependency walker, like Jan said, I assumed it's these two dll files that cause the problem and copied the two dll files into a directory included in the system path. Now it works, independent of where the matlab working directory points to.
Thank you so much!!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Jan
Jan am 1 Apr. 2019
Bearbeitet: Jan am 2 Apr. 2019
The message
"The specified module could not be found"
means, that the mex function cannot find a required library, most likely a run-time-lib of the compiler. You can use the DependencyWalker to check this, see e.g. https://www.mathworks.com/matlabcentral/answers/92362-how-do-i-determine-which-libraries-my-mex-file-or-stand-alone-application-requires
Note than "Invalid MEX-file 'C:\somepath\mexfilename.mexw64': ..." implies, that the file has been found successfully. Otherwise it would be impossible for Matlab to determine, that the file is invalid.
"when I set the working directory to the location where the function and the mex file are, everything works"
Yes, this is strange. Maybe the MEX function uses libraries in the same folder, but this folder is not included in the search path of the operating system? This can be examined with the DepencyWalker also.

5 Kommentare

I came across the dependency walker a few times when trying to find a solution without posting a question, also from one of your own answers. I didn't try it because I thought the problem lies elsewhere.
There are two dll files in the same folder as the function and the mex file. Trying to avoid installing and running dependency walker (which sounds like a time intensive task), I just tried including these two dll files in a directory lying on the system path, now it works!!
I'd like to take this opportunity to thank you, not just for this answer, but for the many many times you have helped me already through the answers you have given to other people.
I don't really know which answer to accept, since both you and Philip Borghesani pointed me into the right direction. I'd accept both answers if I could, but unfortunately that's not possible (as far as I know).
I hope you won't mind that I chose his answer in the end, because he offered the more direct solution.
Thank you so much again.
Jan
Jan am 2 Apr. 2019
Bearbeitet: Jan am 2 Apr. 2019
You are welcome. I'm happy, when your problem is solved.
Starting and running the DependencyWalker takes a few seconds only. The missing libraries are marked in red such that the analysis is easy.
I'd send a message to the authors and ask, if they could insert a meaningful error message, if the DLLs are not found.
That's a good idea. Including plenty of descriptive error messages and/or warnings never hurts. I'll try to contact the developers to tell them.
Thanks again!!
I suspect there is not much the author can do about it. The message/error occurs while loading a dll but before any code in it has run. At one point I tried to improve MATLAB's error message in this situation and there are no simple, robust solutions.
Too bad.
Thank you for letting us know!

Melden Sie sich an, um zu kommentieren.

Bruno Luong
Bruno Luong am 1 Apr. 2019
If the MEX file is compiled with MSVS you might need to install the appropriate Redistributable on the target PC.

Community Treasure Hunt

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

Start Hunting!

Translated by