Generate Shared Utility Code for Custom Data Types
By
default, if a model employs a custom data type (such as a Simulink.AliasType object or an enumeration class), the code generator places
the corresponding type definition (typedef) in the
file. When you generate
code from multiple models, each model duplicates the type definition. These duplicate
definitions can prevent you from compiling the bodies of generated code together.model_types.h
However, you can configure the code generator to place a single type definition in a header
file in the _sharedutils folder. Then, when you generate code from a
model, if the type definition already exists in the _sharedutils folder,
the code generator does not duplicate the definition, but instead reuses it through
inclusion (#include).
Through this mechanism, you can share:
Simulink® data type objects that you instantiate from the classes
Simulink.AliasType,Simulink.Bus, andSimulink.NumericType. For basic information about creating and using these objects, see Manage Replacement of Simulink Data Types in Generated Code andSimulink.Bus.Enumerations that you define, for example, by authoring an
enumclass in a script file or by using the functionSimulink.defineIntEnumType. For basic information about defining enumerations in Simulink, see Use Enumerated Data in Simulink Models.
To share a custom data type across multiple models:
Define the data type. For example, create the
Simulink.AliasTypeobject.Set data scope and header file properties to specific values that enable sharing.
For a data type object, set the
DataScopeproperty to'Exported'and, optionally, specify the header file name through theHeaderFileproperty.For an enumeration that you define as an
enumclass in a script file, implement thegetDataScopemethod (with return value'Exported') and, optionally, implement thegetHeaderFilemethod.For an enumeration that you define by using the
Simulink.defineIntEnumTypefunction, set the'DataScope'pair argument to'Exported'and, optionally, specify the'HeaderFile'pair argumentUse the data type in the models.
Before generating code from each model, set the model configuration parameter Shared code placement to
Shared location.Generate code from the models.
Note
You can configure the definition of the custom data type to appear in a header file in the
_sharedutilsfolder. The shared utility functions that the model build generates into the_sharedutilsfolder do not use the custom data type name. Only model code located in code folders for each model uses the custom data type name.