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
enum
class 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.AliasType
object.Set data scope and header file properties to specific values that enable sharing.
For a data type object, set the
DataScope
property to'Exported'
and, optionally, specify the header file name through theHeaderFile
property.For an enumeration that you define as an
enum
class in a script file, implement thegetDataScope
method (with return value'Exported'
) and, optionally, implement thegetHeaderFile
method.For an enumeration that you define by using the
Simulink.defineIntEnumType
function, 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
_sharedutils
folder. The shared utility functions that the model build generates into the_sharedutils
folder do not use the custom data type name. Only model code located in code folders for each model uses the custom data type name.