Documentation Center

  • Trial Software
  • Product Updates

Generated Code Files for Targets You Build

S-Function MEX-Files

If you have a Simulink® model named mainModel, which contains two Stateflow® blocks named chart1 and chart2, you have a machine named mainModel that parents two charts named chart1 and chart2.

When you simulate the Stateflow chart for mainModel, you generate code for mainModel that compiles into an S-function MEX-file. MEX-file extensions are platform-specific, as described in the MATLAB® software documentation. For example, on 32-bit Windows® PC platforms, you generate a MEX-file for mainModel named mainModel_sfun.mexw32. On Linux® x86-64 platforms, you generate mainModel_sfun.mexa64.

S-function MEX files appear in the current MATLAB folder. You can change this location at the MATLAB command prompt with a cd command.

Folder Structure of Generated Files

Most of the code files that you generate reside in a subfolder of the current MATLAB folder. This table summarizes the default folder structure for different targets.

Target TypeModel TypeFolder Under <pwd>/slprj/_sfprj/<mainModel>
SimulationMain (nonlibrary)/_self/sfun/src
SimulationLibrary/<libModel>/sfun/src
EmbeddableMain (nonlibrary)/_self/rtw/<sys_targ>/src
EmbeddableLibrary/<libModel>/rtw/<sys_targ>/src

These definitions apply to the table:

  • <pwd> is the current working folder.

  • <mainModel> is the name of the main model.

  • <libModel> is the name of the library model.

  • <sys_targ> is the type of embeddable target (for example, grt or ert).

For embeddable targets, the integrated C code for the entire model resides in the subfolder <mainModel>_<sys_targ>_rtw of the current MATLAB folder. The executable file generated for the entire model resides in the current MATLAB folder.

    Tip   To use a root folder different from <pwd> for storing generated files, open the Simulink Preferences Window and update the File generation control section.

    • For simulation targets, specify Simulation cache folder.

    • For embeddable targets, specify Code generation folder.

    For more information, see File generation control in the Simulink documentation.

Code Files for a Simulation Target

For a simulation target, you generate these files:

  • <model>_sfun.h is the machine header file. It contains:

    • All the defined global variables needed for the generated code

    • Type definition of the Stateflow machine-specific data structure that holds machine-parented local data

    • External declarations of any Stateflow machine-specific global variables and functions

    • Custom code strings that you specify

  • <model>_sfun.c is the machine source file. It includes the machine header file and all the chart header files (described below) and contains Simulink interface code.

  • <model>_sfun_registry.c is a machine registry file that contains Simulink interface code.

  • cn_<model>.h is the chart header file for the chart chartn, where n = 1, 2, 3, and so on, depending on how many charts your model has (see the following note). This header file contains type definitions of the chart-specific data structures that hold chart-parented local data and states.

  • cn_<model>.c is the chart source file for chartn, where n = 1, 2, 3, and so on, depending on how many charts your model has (see the following note). This source file includes the machine header file and the corresponding chart header file and also contains:

    • Chart-parented data initialization code

    • Chart execution code (state entry, during, and exit actions, and so on)

    • Chart-specific Simulink interface code

        Note:   Every chart is assigned a unique number at creation time. This number appears as a suffix for the chart source and chart header file names for every chart (where n = 1, 2, 3, and so on, depending on how many charts your model has).

For library models, a static library file named <libModel>_sfun resides in the same folder as the source code. The file extension depends on the platform. On a Windows operating system, the library file is <libModel>_sfun.lib, but on a UNIX® operating system, the library file is <libModel>_sfun.a.

Code Files for an Embeddable Target

For an embeddable target, you generate integrated C code for the entire model:

  • <model>.h

  • <model>.c

You also generate intermediate code files during the target building process:

  • <model>_rtw.tlh

  • <model>_rtw.tlc

  • cn_<model>.tlh, where n = 1, 2, 3, and so on, depending on how many charts your model has

  • cn_<model>.tlc, where n = 1, 2, 3, and so on, depending on how many charts your model has

Other auxiliary files can appear depending on the type of embeddable target you choose for code generation.

Makefiles

You generate makefiles for your model that are platform and compiler-specific. On UNIX platforms, you generate a gmake-compatible makefile named <mainModel>_sfun.mku that compiles all your generated code into an executable. On PC platforms, you generate an ANSI-C compiler-specific makefile based on your C-MEX setup:

CompilerMakefileSymbol Definition File
Microsoft® Visual C++®<mainModel>_sfun.mak<mainModel>_sfun.def (required to build S-function MEX-files)
lcc-win32 (default ANSI-C compiler)<mainModel>_sfun.lmkNone

Was this topic helpful?