17

I am trying to write some mex code but it is painful to debug it on the console with gbd. Is it possible to use Eclipse or the GUI of Matlab? If these are not feasible methods, what is the best way of writing mex code that provides good debug capabilities?

Shai
  • 111,146
  • 38
  • 238
  • 371
erogol
  • 13,156
  • 33
  • 101
  • 155

3 Answers3

8

On Windows platform:
The best way (to my experience) to debug a mex code is to use Visual studio. Here's a link on how to do it.

Other platforms (Linux/Mac):
I'm afraid I haven't find any good way to do so apart from gdb (which is not fun at all).

Update (2018):

Recently, Mathworks released a blog post describing how to use Visual Studio Code to debug mex code.
I haven't tried it myself, but it seems like a nice cross-platform solution for debugging mex files.

Community
  • 1
  • 1
Shai
  • 111,146
  • 38
  • 238
  • 371
8

Debugging C/C++ MEX files in gdb is already comprehensively covered in the official documentation, so my suggestion is to try and integrate gdb with Eclipse CDT.

There's plenty of information out there about how to do it properly, so I'm not going to repeat everything here. The best tutorial I know is IBM's two-part guide, "Interfacing with the CDT debugger":

  1. Part 1: Understand the C/C++ debugger interface - covers the high-level basics.
  2. Part 2: Accessing gdb with the Eclipse CDT and MI - explains how to make Eclipse work with gdb.

I think part 2 is what you really need. Basically, you have to install a plug-in and configure it in Eclipse.

There are also quite a few related questions about this even here on StackOverflow:

  1. How do I use GDB in Eclipse for C/C++ Debugging?
  2. How to install GDB debugger to Eclipse CDT
  3. Debugging with Eclipse CDT and GDB
  4. How can I enter commands to a gdb prompt while debugging with Eclipse CDT?

I haven't tried it out yet, so I hope this works.

Community
  • 1
  • 1
Eitan T
  • 32,660
  • 14
  • 72
  • 109
  • You have my thanks for the answer... I am using the way of official doc on linux but it is so time consuming compared to the GUI way of debugging. But I will take a look to the links you give. – erogol Jan 27 '13 at 22:24
  • @Erogol - I put a bounty on your question hoping to get better answers, especially for eclipse + gdb + linux configuration. Please pay attention to this question and answers during the bounty period, so we can find a working solution together. Thank you. – Shai Jan 28 '13 at 07:19
  • @EitanT - I was kind of hoping for a more detailed method for mex debug using eclipse on Linux. well, nobody else raised to the challenge so there you go, use this bounty well. – Shai Feb 01 '13 at 07:03
  • @Shai I don't access to a MATLAB installed on Linux, unfortunately. I did, however, successfully managed to debug gdb in Eclipse using the instructions above, so I suppose that this combined with MATLAB's tutorial of debugging with gdb makes an adequate answer. If time allows, I will refine it to provide a better explanation. – Eitan T Feb 01 '13 at 17:20
  • The linked IBM "guide" doesn't really help. It is more geared towards implementing one's own debugger plug-in for Eclipse. For those of us just looking for a plug-in that someone else has implemented, this answer is not useful at all, in my opinion. – robguinness Jun 17 '13 at 14:51
  • @robguinness I'm sorry to hear that, but as you might have noticed, my answer is yet untested. Did you try to install the gdb plug-in mentioned in the IBM guide? Have you taken a look at the related questions mentioned? – Eitan T Jun 17 '13 at 15:10
7

Here is my solution which works in Ubuntu 12.04, Matlab R2012b, and Eclipse IDE for C/C++ Developers Kepler Release.

  1. In Eclipse open "Debug Configurations" from the run tab. Then make a new "C/C++ Application".
  2. In the "Main"-tab write the correct path to the matlab executable in the "C/C++ Application" (mine is "/usr/local/MATLAB/R2012b/bin/glnxa64/MATLAB" NOTE: not MATLABPATH/bin/matlab). Main tab
  3. In the "Arguments"-tab add "-nojvm" as program arguments. Arguments
  4. In the "Environment"-tab add variable "MATLAB_DEBUG" with value "gdb" Environment
  5. Press "Apply" and "Debug"
  6. A "Matlab console" will open in the "Console" view.
  7. Write "dbmex on" in this console. This enables debugging.
  8. Run the mex file! (Matlab will stop at loading of each new mex file)
  9. Have fun debugging! :-)
Torstein I. Bø
  • 1,359
  • 4
  • 14
  • 33
  • Tried this with eclipse+Mingw64 in windows and it doesn't work. I get to step nr 7 and it displays "DBMEX doesn't work on the PC. See the MATLAB External Interfaces Guide for details on how to debug MEX-files." – payala Mar 18 '15 at 12:04
  • I have not tested this in windows. There visual studio is much easier. – Torstein I. Bø Mar 19 '15 at 03:31
  • Thanks! Works for me on ArchLinux with R2016a and Eclipse neon 4.6. I just added as arguments also `-r "dbmex on" ` to avoid typing it every time. – bonanza Aug 18 '16 at 08:15
  • Addendum: As path I am using `.../R2016a/bin/glnxa64/MATLAB` i.e. I am not calling the wrapper script in `/usr/bin/...` because eclipse was complaining about it. – bonanza Aug 18 '16 at 08:35