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

19 views (last 30 days)

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 12 Aug 2020
Edited: MathWorks Support Team on 12 Aug 2020
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).
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:

More Answers (0)

Products


Release

R2006b

Community Treasure Hunt

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

Start Hunting!