Main Content

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 model_types.h 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 _sharedutils folder, the code generator does not duplicate the definition, but instead reuses it through inclusion (#include).

Through this mechanism, you can share:

To share a custom data type across multiple models:

  1. Define the data type. For example, create the Simulink.AliasType object.

  2. 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 the HeaderFile property.

    For an enumeration that you define as an enum class in a script file, implement the getDataScope method (with return value 'Exported') and, optionally, implement the getHeaderFile 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 argument

  3. Use the data type in the models.

  4. Before generating code from each model, set the model configuration parameter Shared code placement to Shared location.

  5. Generate code from the models.


    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.

Related Topics