How can we debug our matlab-made DLL used in a C++ app?

208 views Asked by At

We have matlab .m files which have been compiled to a DLL using mcc. This is used by a C++ GUI application which we debug in Visual Studio. When we reach the call to our matlab function, all we can do is step over it. How can we debug, at a source code level, the .m code?

I figured it would be easy to add some 'print' statements, which in matlab are actually called 'disp', and watch stuff scroll by in VS's output window. But we see nothing. Not even from printf() in the C++ source, or when running our app directly from a command line instead of in VS. From Google and S.O., we learn that nothing sent to stdout ever shows up anywhere. (This question is relevant: Visual Studio 2012 C++ Standard Output but didn't work for us.)

I tried msgbox('blah blah') in the .m scripts, and rebuilt the DLL. Nothing came up. I'm not sure if this should work but our matlab code didn't execute, or if msgbox isn't expected to work in this situation, like stdout.

We'd be happy with print-based debugging. Breakpoints and other techniques would be nice, but not necessary at this time.

We use Matlab R2016b on Win7, Visual Studio 2015. We have the .m source used to make the DLL, and we have the source for the GUI app using the DLL.

1

There are 1 answers

0
Ofek Shilon On

Several items:

  1. When you call the initialization routine of your dll, call the generated 'WithHandlers' version: YourDllInitWithHandlers( **, **). The '**' are placeholders for functions accepting the matlab disp/warn/error messages, that you can route to stdout, OutputDebugString or log as you wish.

  2. Did you possibly initialize the mcr from your code with the -nojvm option? If so, messageboxes won't be displayed.

  3. You can't step into the .m sources of compiled libraries. Two options we use are: (a) save the inputs to your compiled-m code as mat files, then load and debug at your leisure at Matlab. (b) Toggle between calling into a compiled-dll and calling into Matlab-Engine. This API-set allows you to launch, control, and call into a fully interactive matlab desktop. You could set breakpoints and step as you would in a regular matlab session. This requires some more setup code, but is very convenient for debugging.