Skip to Main Content Skip to Search
Home |   Select Country  Choose Country  |  Contact Us  |  Cart Store 
Create Account | Log In
Products & Services Solutions Academia Support User Community Company
spacer spacer spacer spacer spacer spacer

Technical Solutions

How do I debug a MATLAB C/C++ MEX-file in Microsoft Visual Studio 2005?


Date Last Modified: Friday, June 26, 2009
Solution ID:   1-3KK6RK
Product:   MATLAB
Reported in Release:   R2006b
Fixed in Release:   R2007b
Platform:   Windows
Operating System:   Windows Any
 

Subject:

How do I debug a MATLAB C/C++ MEX-file in Microsoft Visual Studio 2005?

Problem Description:

I would like to debug a C or C++ MEX-file in Microsoft Visual Studio 2005.

Solution:

This Solution is specific to the Microsoft Visual Studio 2005 debugger on Windows, and works on MATLAB 7.3 (R2006b) or later. Note that this process will not work if you only have Visual C++ installed (and not Visual Studio). For other configurations, see the Debugging section of Tech Note 1605, MEX-files Guide:
http://www.mathworks.com/support/tech-notes/1600/1605.html

1. To debug a C/C++ MEX-file from the Microsoft Visual Studio 2005, you must first choose the Microsoft Visual C/C++ version 8.0 compiler in MATLAB, using the command:
mex -setup
2. Compile the MEX-file with the -g option, which 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.
3. Start the Microsoft Visual Studio 2005 Development Environment. Do not exit your MATLAB session.
4. In Microsoft Development Environment, select Tools->Attach to Process... from the menu bar.
5. In the Processes dialog box, select the MATLAB process and click Attach.
6. 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 selecting Breakpoint ->Insert Breakpoint from 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.
7. 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.

A more specific writeup of the above procedure, along with instructions on how to debug with gdb on Linux, is available in the MATLAB documentation here:
http://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk/help/techdoc/matlab_external/f32489.html

Please provide feedback to help us improve this Solution
Contact support
E-mail this page
Print this page