Generate Code and Executables for Individual Subsystems
You can generate code and build a standalone executable for a subsystem within a model. When you generate code for an individual subsystem, the code generation and build process uses the code generation and build parameters of the root model.
Open a model with a subsystem, such as
SubsystemAtomic
.openExample('SubsystemAtomic')
Open Simulink® Coder™ or Embedded Coder®.
If you are using Embedded Coder, from the C Code tab, click View Code to open the Code View editor.
Right-click the Subsystem block. From the context menu, click Block Parameters (Subsystem). From the Block Parameters: SS1 window, open the Code Generation tab.
Use the Function name options and File name options parameters to control the names of the generated functions and files. Set the Function name options parameter to
Auto
so that the code generator uses the name of the subsystem in the generated file names.Right-click the Subsystem block. From the context menu, select C/C++ Code > Build This Subsystem.
To configure properties such as the name, class, and storage class of variables (or data objects) that are referenced as block parameters in the subsystem, convert the subsystem to a Model block. Configure the properties through the Code Mappings editor.
For more information on tunable and inlined parameters and storage classes, see Create Tunable Calibration Parameter in the Generated Code.
The build process displays status messages in the MATLAB® Command Window. When the build is complete, the generated executable is in your working folder. The name of the generated executable is
(on PC platforms) orsubsystem
.exe
(on The Open Group UNIX® platforms).subsystem
is the name of the source Subsystem block. In this example, the Subsystem block is namedsubsystem
SS1
.The generated code is in a build subfolder named
.subsystem
_target
_rtw
is the name of the source subsystem block andsubsystem
is the name of the target configuration.target
Subsystem Build Limitations
These limitations apply to building subsystems:
You cannot use a subsystem build for a model configured with an ERT-based system target file and service code interface.
A subsystem build does not support a subsystem that has a function-call trigger input or a function-call output.
When you build a subsystem that includes an Outport block for which the Data type parameter specifies a bus object, you must address errors that result from setting signal labels. To configure the software to display these errors, in the Configuration Parameters dialog box for the parent model, on the Diagnostics > Connectivity pane, set the Signal label mismatch parameter to
error
.When building a subsystem, the code generator inserts traceability tags for root-level Inport and Outport blocks in place of subsystem Inport and Outport blocks, which are virtual (see Nonvirtual and Virtual Blocks). For instance:
However, you cannot trace these root-level tags to the Inport and Outport blocks of the root-level model./* Outport: '<Root>/Out1' incorporates: * Inport: '<Root>/In1'
When a subsystem is in a triggered or function-call subsystem, the right-click build process might fail if the subsystem code is not sample-time independent. To determine whether a subsystem is sample-time independent:
Copy all blocks in the subsystem to an empty model.
In the Configuration Parameters dialog box, on the Solver pane, set:
Type to
Fixed-step
.Periodic sample time constraint to
Ensure sample time independent
.Click Apply.
Update the model. If the model is sample-time dependent, Simulink generates an error in the process of updating the diagram.
When you use the right-click build process for a subsystem, the code generator attempts to use the subsystem name for generated code files. In some cases, there can be a conflict with the name that you specify when you set, for example, File name options to
Use function name
or Function name options toUse subsystem name
. You see an error:The subsystem 'model/subsys' is trying to generate code to an reserved file (subsys) for the model 'subsys'...
To resolve the error, modify one of the conflicting file names so that the names are unique.
In a subsystem build warning, the subsystem block path hyperlink that is created references a temporary model block path instead of the actual model block path. In the Diagnostic Viewer, clicking the subsystem hyperlink does not take you to the block. In the Command Window, you see a message:
... No system or file called 'subsystemName' found. ...
Related Examples
- Generate Predictable Function Interface to Match Graphical Block Interface (Embedded Coder)
- Generate Subsystem Code as Separate Function and Files