Main Content

Generate Code and Executables for Individual Subsystems

You can generate code and build an executable for a subsystem within a model. The code generation and build process uses the code generation and build parameters of the root model.

  1. In the Configuration Parameters dialog box, set up the code generation and build parameters, similar to setting up the code generation for a model.

  2. Right-click the Subsystem block. From the context menu, select C/C++ Code > Build This Subsystem from the context menu.

    Alternatively, in the current model, click a subsystem and then from the C Code tab, click Build.


    When you select Build This Subsystem, if the model is operating in external mode, the build process automatically turns off external mode for the duration of the build. The code generator restores external mode upon completion of the build process.

    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 and configure the properties using the Code Mappings editor.

    For more information on tunable and inlined parameters and storage classes, see Create Tunable Calibration Parameter in the Generated Code.

  3. 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 subsystem.exe (on PC platforms) or subsystem (on The Open Group UNIX® platforms). subsystem is the name of the source subsystem block.

    The generated code is in a build subfolder, named subsystem_target_rtw. subsystem is the name of the source subsystem block and target is the name of the target configuration.

When you generate code for a subsystem, you can generate an S-function by right-clicking the Subsystem block and selecting C/C++ Code > Build This Subsystem from the context menu. For more information on S-functions, see Generate S-Function from Subsystem.

Subsystem Build Limitations

The following limitations apply to building subsystems:

  • Subsystem build does not support a subsystem that has a function-call trigger input or a function-call output.

  • When you right-click a Subsystem block and select C/C++ Code > Build This Subsystem from the context menu to 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 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 find out whether a subsystem is sample-time independent:

    1. Copy all blocks in the subsystem to an empty model.

    2. In the Configuration Parameters dialog box, on the Solver pane, set:

      1. Type to Fixed-step.

      2. Periodic sample time constraint to Ensure sample time independent.

      3. Click Apply.

    3. 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 to Use 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.