MATLAB: How to debug a MATLAB 7.4 (R2007a) C/C++ MEX-file in Intel Visual C++ 9.1 using Microsoft Visual Studio 2005

8.0ivcMATLABmsvc

I would like to debug a C/C++ MEX-file built with Intel Visual C++ 9.1 using Microsoft Visual Studio 2005.

Best Answer

1. To debug a MEX-file from within MATLAB, you must first compile the MEX-file with the -g option to the mex function. The -g option builds the MEX-file with debugging symbols included. For example:
mex -g yprime.c
On a 32–bit platform, this command creates the executable file yprime.mexw32.
2. Start the Microsoft Visual Studio 2005 Development Environment. Do not exit your MATLAB session.
3. On the Visual Studio Tools menu, select Attach to Processes. In the Processes dialog box, select the MATLAB process and click Attach.
4. Open the source files by selecting File > Open > File. Set a breakpoint on the desired line of code by right-clicking the line of code and clicking Insert Breakpoint on the context menu. If you have not yet run the executable file, ignore any "?" that appears with the breakpoint next to the line of code.
5. In MATLAB, start the executable file, which runs in the Microsoft debugging environment:
yprime(1,1:4)
For more information on how to debug in the Microsoft environment, refer to the Microsoft Development Environment documentation.