Code Generation of Referenced Models

This section describes model referencing considerations that apply specifically to code generation by the Simulink® Coder™. This section assumes that you understand referenced models and related terminology and requirements, as described in Model Reference Basics (Simulink) and associated topics.

When generating code for a referenced model hierarchy, the code generator produces a stand-alone executable for the top model, and a library module called a model reference target for each referenced model. When the code executes, the top executable invokes the model reference targets to compute the referenced model outputs. Model reference targets are sometimes called Simulink Coder targets.

Be careful not to confuse a model reference target (Simulink Coder target) with other types of targets:

  • Target hardware — A platform for which the Simulink Coder software generates code

  • System target — A file that tells the Simulink Coder software how to generate code for particular purpose

  • Rapid Simulation target (RSim) — A system target file supplied with the Simulink Coder product

  • Simulation target — A MEX-file that implements a referenced model that executes with Simulink Accelerator™ software

The code generator places the code for the top model of a hierarchy in the code generation folder (Simulink) and places the code for referenced models in an slprj folder in the code generation folder (Simulink). Subfolders in slprj provide separate places for different types of files. For folder information, see Manage Build Process Folders.

By default, the product uses incremental code generation. When generating code, it compares structural checksums of referenced model files with the generated code files to determine whether to regenerate model reference targets. To control when rebuilds occur, use the configuration parameter Model Referencing > Rebuild. For details, see Rebuild (Simulink).

In addition to incremental code generation, the Simulink Coder software uses incremental loading. The code for a referenced model is not loaded into memory until the code for its parent model executes and needs the outputs of the referenced model. The product then loads the referenced model target and executes. Once loaded, the target remains in memory until it is no longer used.

Most code generation considerations are the same whether or not a model includes referenced models: the code generator handles the details automatically insofar as possible. This chapter describes topics that you may need to consider when generating code for a model reference hierarchy.

If you have a Embedded Coder® license, custom targets must declare themselves to be model reference compliant if they need to support Model blocks. For more information, see Support Model Referencing.