MATLAB: Can I compile a MEX function with -g flag and debug it on other computer

debugMATLABmexvs

In the following article, it states that "Binary MEX files built with the -g debugging option do not execute on other computers because they rely on files that are not distributed with MATLAB":
Specifically, I would like to compile a MEX function with the "-g" flag and then debug it on a different machine. Would this be impossible, even if the machines had the same operating system and were running the same version of MATLAB?

Best Answer

It is possible to compile a MEX file with "-g" and debug it on a different machine, although it is not guaranteed to work for every MEX file and debugger. For example, the following workflow was accomplished on Windows 10, MATLAB R2019b, and VS Code:

1) Use MATLAB to compile a MEX file with the "-g" flag:

>> mex -g sourceCode.c

2) Open the source code in Visual Studio Code and step through it with the gdb debugger after MATLAB calls the MEX function. To set up VS code debugging, use this tutorial from the MathWorks blog:

https://blogs.mathworks.com/developer/2018/06/19/mex-debugging-vscode/

Note that this tutorial requires you to install the "MATLAB Support for MinGW-w64 C/C++ Compiler" support package.

3) Transfer the source code and the compiled MEX file to a different computer with the same operating system and place both files in a folder with the same name and path as on the first computer.

4) Without recompiling the MEX file, open the source code in VS Code and step through it after MATLAB calls the MEX function.