- yprime.c (source file)
- yprime.mexw64 (binary MEX file)
- yprime.mexw64.pdb (debugging symbols)
How can I debug a C MEX file on Microsoft Windows Platforms with Microsoft Visual Studio?
114 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
MathWorks Support Team
le 3 Mai 2019
Modifié(e) : MathWorks Support Team
le 6 Sep 2024 à 15:40
How can I debug a C MEX file with Microsoft® Visual Studio®?
Réponse acceptée
MathWorks Support Team
le 6 Sep 2024 à 0:00
Modifié(e) : MathWorks Support Team
le 19 Août 2023
The instructions below were tested with Visual Studio Professional 2017, 2019, and 2022. If you are using a different version of Visual Studio, refer to the corresponding Microsoft documentation.
1. Make sure Visual Studio is your selected C compiler in MATLAB®:
>> cc = mex.getCompilerConfigurations('C','Selected');
>> cc.Name
ans =
'Microsoft Visual C++ 2022 (C)'
To change the selected C compiler in MATLAB, use:
>> mex -setup C
2. Compile the source MEX file in MATLAB with the -g option, which builds the file with debugging symbols included. For example:
>> copyfile(fullfile(matlabroot,'extern','examples','mex','yprime.c'),'.','f')
>> mex -g yprime.c
You should now have three files in your current folder:
3. Start Visual Studio. Do not exit your MATLAB session. In case you see a "Get started" screen, select "Continue without code".
4. Click the Attach... button the Visual Studio toolbar:
Or from the Visual Studio Debug menu, select Attach to Process....
NOTE: If you do not have the Debug menu, your Visual Studio installation may be missing the Debugging functionality. Reach out to your IT helpdesk to install this feature.
5. In the "Attach to Process" dialog box, select the MATLAB process:
Ensure that the Attach to: parameter is set to Automatic: Native Code. Then click "Attach".
6. Open the source file "yprime.c" in Visual Studio by selecting File > Open > File. Locate "yprime.c" in the folder matching your MATLAB workspace.
7. Set a breakpoint by right-clicking the desired line of code and following Breakpoint > Insert Breakpoint in the context menu. It is often convenient to set a breakpoint at "mexFunction" to stop at the beginning of the gateway routine.
NOTE: If you have not yet run the executable file, you can ignore any “!” icon that appears with the breakpoint next to the line of code, showing the following warning:
"The breakpoint will not currently be hit. No symbols have been loaded for this document."
Once you hit one of your breakpoints, you can make full use of any commands the debugger provides to examine variables, display memory, or inspect registers.
8. Go back to MATLAB and type:
>> yprime(1,1:4)
9. Once you select Debug > Continue in Visual Studio, MATLAB displays:
ans =
2.0000 8.9685 4.0000 -1.0947
For more information on how to debug in the Visual Studio environment, see your Microsoft documentation.
1. To avoid stepping into a MathWorks-provided function, use "Step Over" on lines that contain calls to MathWorks APIs.
2. To avoid stepping out into MathWorks code at the end of your custom function, use the "continue" (play button) when at the end of your function.
3. Do not enable "Access Violation" for handling exceptions when debugging MEX files. To avoid breaking at this exception, clear the check box for Win32 Exceptions. Look for the check box in Debug > Windows > Exception Settings....
4. Binary MEX files built with the -g option do not execute on other computers because they rely on files that are not distributed with MATLAB. For more information on isolating problems with MEX files, see Troubleshoot MEX Files.
5. Debugging MEX files may not work when MATLAB is started from third-party software such as dSPACE or Xilinx System Generator. Please use the regular way to start MATLAB.
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Write C Functions Callable from MATLAB (MEX Files) dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!