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 (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
Generate C++ Class Interface to Model Code
To generate encapsulated C++ class code from a model:
Set model configuration parameter Language 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
Errorunless 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
andmodel.h. For example, the following code excerpt from the H file generated for the example modelmodel.cppSecondOrderSystemshows 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.
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++ classcode 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