Main Content

Storage Classes for Code Generation from MATLAB Code

If you have an Embedded Coder® license, you can use storage classes to control the declaration and definition of a global variable in the generated C/C++ code.

In the context of code generation, a storage class is a specification that determines the declaration and definition of a variable in the generated code. For code generation, the term storage class is not the same as the C language term storage class specifier.

Storage classes help you to integrate generated code with external code. You can make a generated variable visible to external code. You can also make variables declared in the external code visible to the generated code. For code generation from MATLAB® code, you can use storage classes with global variables only. The storage class determines:

  • The file placement of a global variable declaration and definition.

  • Whether the global variable is imported from external code or exported for use by external code.

To assign a storage class to a global variable, in your MATLAB code, use the coder.storageClass function. Only when you use an Embedded Coder project or configuration object for generation of C/C++ libraries or executables does the code generator recognize coder.storageClass calls.

The syntax for coder.storageClass is:

coder.storageClass(global_name, storage_class)

global_name is the name of a global variable, specified as a character vector. global_name must be a compile-time constant.

storage_class can be one of the following values.

Storage ClassDescription
'ExportedGlobal'
  • Defines the variable in the Variable Definitions section of the C file entry_point_name.c.

  • Declares the variable as an extern in the Variable Declarations section of the header file entry_point_name.h

  • Initializes the variable in the function entry_point_name_initialize.h.

'ExportedDefine'

Declares the variable with a #define directive in the Exported data define section of the header file entry_point_name.h.

'ImportedExtern'

Declares the variable as an extern in the Variable Declarations section of the header file entry_point_name_data.h. The external code must supply the variable definition.

'ImportedExternPointer'

Declares the variable as an extern pointer in the Variable Declarations section of the header file entry_point_name_data.h. The external code must define a valid pointer variable.

Storage classes have these requirements and limitations:

  • Assign the storage class to a global variable in a function that declares the global variable. You do not have to assign the storage class in more than one function.

  • After you assign a storage class to a global variable, you cannot assign a different storage class to that global variable.

  • You cannot assign a storage class to a constant global variable.

  • A global variable with an ExportedDefine storage class must be a scalar but not a complex or multi-word scalar. The global variable must only be read and not written to in the code.

If you do not assign a storage class to a global variable, except for the declaration location, the variable behaves like it has an 'ExportedGlobal' storage class. For an 'ExportedGlobal' storage class, the global variable is declared in the file entry_point_name.h. When the global variable does not have a storage class, the variable is declared in the file entry_point_name_data.h.

See Also

Related Topics