Monday, June 16, 2025
HomeMatlabDebugging C/C++ code in Simulink » Man on Simulink

Debugging C/C++ code in Simulink » Man on Simulink


As you in all probability know, it’s doable to combine C/C++ code in Simulink simulations. This may be achieved in some ways, together with:

  • C/C++ S-Perform
  • S-Perform Builder
  • Legacy Code Device
  • C Caller
  • C Perform
  • MATLAB Perform
  • MATLAB System
  • Stateflow chart
A very long time in the past, I printed a weblog put up describing find out how to debug C/C++ S-Capabilities. At the moment, the method was advanced and required you to manually mex the S-Perform with debug flags and fix the debugger to the MATLAB course of.

In current releases, now we have drastically simplified the method. On this put up, I’ll spotlight how this may be achieved in two methods:

Preliminary Setup

This web page lists the supported blocks together with the compilers and IDEs supported for various working techniques.

On Home windows, to make use of the Microsoft Visible Studio debugger, you’ll seemingly must set a Home windows registry key. You’ll be able to see if this registry key entry is already configured by executing this line of code:

winqueryreg(‘HKEY_LOCAL_MACHINE’,‘SYSTEMCurrentControlSetControlSession Supervisor’,‘DebuggerMaxModuleMsgs’)

Set Breakpoints in Customized Code

As an instance this course of, I used the next easy C file and header:

I created a brand new mannequin and specified the C file in its configuration:

I then switched to the Embody headers part and used the Auto-fill from supply information button to routinely fill the embrace headers textual content:

I added a C Caller block to the mannequin and will now choose my operate in its dialog:

I linked inputs and outputs to the C Caller block to have a working simulation:

In MATLAB, I take advantage of mex -setup to make sure that I’ve Visible Studio chosen as compiler.

Within the Debug tab of the Simulink mannequin, I can then click on on Set Breakpoints in Customized Code:

The mannequin standing bar will inform us that it’s launching the exterior debugger

Microsoft Visible Studio will open the C file and routinely connect to the MATLAB course of. At this level, you’ll be able to set breakpoints within the customized code, click on play in Simulink, and examine the C code when the breakpoints is hit:

MinGW and Visible Studio Code

system(“code –install-extension ” +

fullfile(matlabshared.supportpkg.getSupportPackageRoot(),

“toolbox”, “targetframework”, “supportpackage”,

“vscodedebug”, “extension”,

“matlab-coder-interface-vscode-debugging.vsix”))

Debugging customized code in Visible Studio Code is simply supported when the customized code is simulated in a separate course of. Normally, it is a good suggestion to simulate customized code in a separate course of when debugging. That method, if the customized code crashes, it won’t crash the MATLAB session with it. This feature might be enabled right here:

As soon as that is achieved, the method is just like the one described above with Visible Studio. Click on the Set Breakpoints in Customized Code button within the Debug tab of the toolstrip, this may launch Visible Studio Code and fix it to the MATLAB course of. Add the breakpoints within the code and simulate the mannequin.

Now it is your Flip

Are you leveraging this function to set breakpoints and debug your customized C code? Do you like to make use of Visible Studio or Visible Studio Code to debug your C code?



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments