Generate C++ Class Interface to Model or Subsystem Code - MATLAB & Simulink (original) (raw)
Main Content
To generate a C++ class interface to model code, set model configuration parameter Code interface packaging (component) 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:
- Set model configuration parameter to
C++
. This selection also enables C++ class code interface packaging for the model. - Verify that model configuration parameter Code interface packaging is set to
C++ class
. - 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. - Generate code for the model.
- 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 modelSecondOrderSystem
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, seeConfigure 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 > 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 andC++ 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:
- Open the containing model and select the subsystem block.
- Right-click the subsystem and select > .
- 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.
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