Generate Shared Utility Code for Custom Data Types
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.
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
the code generator does not duplicate the definition, but instead reuses it through
Through this mechanism, you can share:
Simulink® data type objects that you instantiate from the classes
Simulink.NumericType. For basic information about creating and using these objects, see Control Data Type Names in Generated Code and
Enumerations that you define, for example, by authoring an
enumclass in a script file or by using the function
Simulink.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
Set data scope and header file properties to specific values that enable sharing.
For a data type object, set the
'Exported'and, optionally, specify the header file name through the
For an enumeration that you define as an
enumclass in a script file, implement the
getDataScopemethod (with return value
'Exported') and, optionally, implement the
For an enumeration that you define by using the
Simulink.defineIntEnumTypefunction, set the
'DataScope'pair argument to
'Exported'and, optionally, specify the
Use the data type in the models.
Before generating code from each model, set the model configuration parameter Shared code placement to
Generate code from the models.
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.