MATLAB: Am I not able to debug an S-function created from a subcomponent in MSVC with the ERT S-Function Target in Real Time Workshop 6.3 (R14SP3)

simulink coder

I would like to be able to debug the S-function (using Visual C++) created with the ERT S-function target when I right-click on a submodel and select Real Time Workshop > Generate S-function.
I added the debug options for VC++ compiling to the make_rtw and I was able to compile/build the DLL, however I was not able to get VC++ to intercept a breakpoint on a line of code when running the debugger.

Best Answer

To be able to use the debugger in MSVC, you need to make two changes in the make file that is generated using the ERT S-Function target. Find the attached model that shows how this is implemented and follow these steps:
1. Open the file SourceModel_orig.mdl, right click on the submodel “Subsystem1”, and select “Real-Time Workshop > Generate S-Function”.
2. When the GUI appears, check “Use Embedded Coder” and then click “Build”.
3. Delete the created .dll or .mexw32 file, e.g. Subsystem_sf.mexw32. If you cannot delete the file, type:
clear mex
at the MATLAB command prompt, then retry the deletion.
3. Change the directory to Subsystem_ert_rtw, and delete:
Subsystem.obj
Subsystem.lib
4. Open Subsystem.mk and add “-Zd” in the CFLAGS option located in the “Compiler and linker option” section at line 218, for example:
CFLAGS = -Zd $(CC_OPTS) $(CPP_REQ_DEFINES) $(USER_INCLUDES) \
$(cflags) $(CVARSFLAG)
Attached is a template file, Subsystem_template.mk, that you can use as a reference.
5. In line 328 located in the section “#--- Model reference SIM Target ---“,
add “–g” at the mex command, for example:
$(MEX) $(MEX_OPT_FILE) -g $(RTW_SFUN_SRC) $(MODELLIB) $(MODELREF_LINK_LIBS) $(SHARED_LIB) $(LIBS) -outdir $(RELATIVE_PATH_TO_ANCHOR)
6. Open a DOS command shell and run the .bat file, for example Subsystem.bat
On the directory where you have your model, the following files should be created
Subsystem_sf.mexw32
Subsystem_sf.mexw32.pdb
7. You can now open Subsystem_sf.c in VC and place a breakpoint, for example at line 39:
static void mdlInitializeSizes(SimStruct *S)
8. Attach MATLAB process in MSVC (select Tools > Debug Process and, then click Attach).
Run the model SourceModel.mdl (not SourceModel_orig.mdl). It should stop at the breakpoint you specified.