Main Content

Generate C++ Class Interface to Model or Subsystem Code

To generate a C++ class interface to model code, set model configuration parameter Code interface packaging to C++ class. The generated interface encapsulates required model data into C++ class attributes and model entry point functions into C++ class methods. The benefits of C++ class encapsulation include:

  • Greater control over access to model data

  • Ability to create multiple instances of model classes

  • Easier integration of model code into C++ programming environments

C++ class encapsulation also works for right-click builds of nonvirtual subsystems. (For information on requirements that apply, see Generate C++ Class Interface to Nonvirtual Subsystem Code.)

Generate C++ Class Interface to Model Code

To generate encapsulated C++ class code from a model:

  1. Set model configuration parameter Language to C++. This selection also enables C++ class code interface packaging for the model.

  2. Verify that model configuration parameter Code interface packaging is set to C++ class.

  3. Examine the setting of Multi-instance code error diagnostic. Leave the parameter at its default value Error unless you need to alter the severity level for diagnostics displayed when a model violates requirements for generating multi-instance code.

  4. Generate code for the model.

  5. Examine the C++ model class code in the generated files model.h and model.cpp. For example, the following code excerpt from the H file generated for the example model SecondOrderSystem shows the C++ class declaration for the model.

    /* Class declaration for model SecondOrderSystem */
    class SecondOrderSystemModelClass {
      /* public data and function members */
     public:
      /* External outputs */
      ExtY_SecondOrderSystem_T SecondOrderSystem_Y;
    
      /* Model entry point functions */
    
      /* model initialize function */
      void initialize();
    
      /* model step function */
      void step();
    
      /* model terminate function */
      void terminate();
    
      /* Constructor */
      SecondOrderSystemModelClass();
    
      /* Destructor */
      ~SecondOrderSystemModelClass();
    
      /* Real-Time Model get method */
      RT_MODEL_SecondOrderSystem_T * getRTM();
    ...
    };

    For more information about generating and calling model entry-point functions, see Configure Generated C Function Interface for Model Entry-Point Functions.

Note

If you have an Embedded Coder® license and you have selected an ERT-based system target file for your model, use additional Code Generation > Interface pane parameters to customize the generated C++ class interface.

Generate C++ Class Interface to Nonvirtual Subsystem Code

You can generate C++ class interfaces for right-click builds of nonvirtual subsystems, if the following requirements are met:

  • The model is configured for the C++ language and C++ class code interface packaging.

  • The subsystem is convertible to a Model block using the function Simulink.SubSystem.convertToModelReference. For referenced model conversion requirements, see the Simulink® reference page Simulink.SubSystem.convertToModelReference.

To configure C++ class interfaces for a subsystem that meets the requirements:

  1. Open the containing model and select the subsystem block.

  2. Right-click the subsystem and select C/C++ Code > Build This Subsystem.

  3. When the subsystem build completes, examine the C++ class interfaces in the generated files and the HTML code generation report. For more information about generating and calling model entry-point methods, see Configure Generated C Function Interface for Model Entry-Point Functions.

If you have an Embedded Coder license and you have selected an ERT-based system target file for your model, you can use the MATLAB® command RTW.configSubsystemBuild to customize the generated C++ class interface to subsystem code.

C++ Class Interface Limitations

  • Among the data exchange interfaces available on the Interface pane of the Configuration Parameters dialog box, only the C API interface is supported for C++ class code generation. If you select External mode or ASAP2 interface, code generation fails with a validation error.

  • If a model root inport value connects to a Simscape™ conversion block, you must insert a Simulink Signal Conversion block between the root inport and the Simscape conversion block. On the Simulink Signal Conversion block parameter dialog box, select Exclude this block from 'Block reduction' optimization.

  • You cannot use a C++ class interface in cases when a referenced model cannot have a combined output/update function. Cases include a model that

    • Has a continuous sample time

    • Saves states

Related Topics