MATLAB: How to debug MEX-file compiled with MinGW64 and -g flags

fileflagsg++MATLAB Compilermexmingw64

How to debug MEX-file compiled with MinGW64 and -g flags

Best Answer

Follow the steps below to debug a MEX-file compiled with MingW64 and the -g flag:
1. Start Eclipse and choose a new workspace.
Download Eclipse from link <https://www.eclipse.org/downloads/packages/eclipse-ide-cc-developers/lunasr2>
2. Use "File" -> "New" -> "Project"... and select "General"->"Project"
3. Choose a name for your project and click "Finish"
4. Use "File"->"New"->"Other"..." and then "General"-> "Untitled Text File" to create a new text file.
Enter the following in the text file 
handle SIGSEGV nostop
Then save the file as "gdbinit" in "mexdebug" folder as shown in the figure.
5. Drag the C/Cpp source code of your MEX-file into the project. When asked to copy or link choose "Link to file" so that the source codes stay in their original locations.
6. Go to "Run" -> "Debug Configurations...". Select "C/C++ Attach to Application" and select "New" button.
7. On the "Debugger" tab enter the location of the file created under 4 as "GDB command file".
 
8. Click "Debug"
9. Select MATLAB.exe from the list.
10. The debugger will suspend MATLAB. Use "Resume" button to continue running MATLAB.
11. Open the C/Cpp code in Eclipse and place a breakpoint.
12. Run the MEX-file in MATLAB
13. When the breakpoint is hit you will stop in the debugger and can for example step through the code and inspect variables.