MEX file debugging with MS Visual Studio

31 views (last 30 days)
Hi all,
I have been working with this project for some time now, which involves creating and using a MEX file within a much larger MATLAB project. Unfortunately the MEX file is pretty big and it is becoming a pain to debug just by inspection, especially since the MEX file works with pointers and gives segmentation errors upon running. This causes MATLAB to crash upon an error as well.
I tried using the MS Visual Studio 2010 and 2008 with MATLAB to debug the file.
I haven't managed to do it. I can attach the Visual Studio to the MATLAB process but when I open the C file in Visual Studio, and try to set a breakpoint, it tells me that "no symbols have been loaded for this file".
(You can see even in the link of the Mathworks site that I have posted above, upon setting the breakpoint the symbols are not loaded as shown by the "empty" breakpoint symbol) :P
I have given it a bit of hit and try for sometime now but still no success. If anyone has any experience please share, coz I am at my wits end. :(
(I remember once I tried debugging in Linux using gdb but that was also impossible, but anyways that is another story ! for the moment I just want to be able to debug in windows )
Thank you very much !
All replies greatly appreciated ... :)

Accepted Answer

Kaustubha Govind
Kaustubha Govind on 1 Mar 2012
When you compile the MEX-file, did you use the mex -g option so that a debuggable binary is built? If using Visual Studio, you should see a PDB (eg. myfile.mexw64.pdb) file created.
  4 Comments
Friedrich
Friedrich on 2 Mar 2012
the "no symbols have been loaded for this file" is expected since a mex file is dynamically loaded. The symbols are loaded at runtime of that mex. So simply set the breakpoint, ignore the message. Run the MATLAB code which uses the mex file and you will stop in that line.
junaid
junaid on 2 Mar 2012
Well I will be damned ... it DOES actually WORK ... Thank you very much Friedrich ... muchas gracias ... much appreciated ...
OMG !!! I wasted so much time trying to debug mex files without a debugger !!! and OMG !!! it really works ...
I think I tried this once before like last July and back then for some reason it didn't work. (I think I was doing everything similar except MAYBE back then I had an express version of Microsoft Visual C++ compiler and now I have the full Visual Studio 2008 ...)
Thanks again guys ...

Sign in to comment.

More Answers (1)

埃博拉酱
埃博拉酱 on 13 Feb 2023
As @Friedrich said, MEX file functions are loaded dynamically, so the function needs to be called once in MATLAB to load symbols. In most cases, the breakpoint can still be hit.
However, there are exceptions. Sometimes the breakpoint cannot be hit, and even after calling the function in MATLAB, the symbol is still displayed as not loaded. In my experience, this is because you chose the wrong process in Visual Studio because sometimes there are multiple processes called MATLAB in the system, and only one of them is correct.
If you don't know which is correct, it is recommended to close MATLAB, use Task Manager to end all MATLAB processes, then reopen them, and reselect the process in Visual Studio (don't use VS's "reattach to process" feature, it will help you select the wrong process again).

Categories

Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!