Main Content

coder.storageClass

Assign storage class to global variable

Description

coder.storageClass(global_name, storage_class) assigns the storage class storage_class to the global variable global_name.

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.

You must have an Embedded Coder® license to use coder.storageClass. Only when you use an Embedded Coder project or configuration object for generation of C/C++ libraries or executables does the code generation software recognize coder.storageClass calls.

example

Examples

collapse all

In the function addglobals_ex, assign the 'ExportedGlobal' storage class to the global variable myglobalone and the 'ExportedDefine' storage class to the global variable myglobaltwo.

function y = addglobals_ex(x) %#codegen
% Define the global variables.
global myglobalone;
global myglobaltwo;

% Assign the storage classes.
coder.storageClass('myglobalone','ExportedGlobal');
coder.storageClass('myglobaltwo','ExportedDefine');
y = myglobalone + myglobaltwo + x;
end

Create a code configuration object for a library or executable.

cfg = coder.config('dll','ecoder', true);

Generate code. This example uses the -globals argument to specify the types and initial values of myglobalone and myglobaltwo. Alternatively, you can define global variables in the MATLAB® global workspace. To specify the type of the input argument x, use the -args option.

codegen -config cfg -globals {'myglobalone', 1, 'myglobaltwo', 2} -args {1} addglobals_ex -report

From the initial values of 1 and 2, codegen determines that myglobalone and myglobaltwo have the type double. codegen defines and declares the exported variables myglobalone and myglobaltwo. It generates code that initializes myglobalone to 1.0 and myglobaltwo to 2.0.

To view the generated code for myglobaltwo and myglobalone, click the View report link.

  • myglobaltwo is defined in the Exported data define section in addglobals_ex.h.

    /* Exported data define */
    
    /* Definition for custom storage class: ExportedDefine */
    #define myglobaltwo                    2.0
    
  • myglobalone is defined in the Variable Definitions section in addglobals_ex.c.

    /* Variable Definitions */
    /* Definition for custom storage class: ExportedGlobal */
    double myglobalone;
  • myglobalone is declared as extern in the Variable Declarations section in addglobals_ex.h.

    /* Variable Declarations */
    /* Declaration for custom storage class: ExportedGlobal */
    extern double myglobalone;
  • myglobalone is initialized in addglobals_ex_initialize.c.

    /* Include Files */
    #include "addglobals_ex_initialize.h"
    #include "addglobals_ex.h"
    #include "addglobals_ex_data.h"
    
    /* Function Definitions */
    
    /*
     * Arguments    : void
     * Return Type  : void
     */
    void addglobals_ex_initialize(void)
    {
      myglobalone = 1.0;
      isInitialized_addglobals_ex = true;
    }

In the function addglobal_im, assign the 'ImportedExtern' storage class to the global variable myglobal.

function y = addglobal_im(x)

% Define the global variable.

global myglobal;

% Assign the storage classes.

coder.storageClass('myglobal','ImportedExtern');
y = myglobal + x;
end

Create a file c:\myfiles\myfile.c that defines and initializes the imported variable myglobal.

#include <stdio.h>

/* Variable definitions for imported variables */ 
double myglobal = 1.0;

Create a code configuration object. Configure the code generation parameters to include myfile.c. For output type 'lib', or if you generate source code only, you can generate code without providing this file. Otherwise, you must provide this file.

cfg = coder.config('dll','ecoder', true);
cfg.CustomSource = 'myfile.c';
cfg.CustomInclude = 'c:\myfiles';

Generate the code. This example uses the -globals argument to specify the type and initial value of myglobal. Alternatively, you can define global variables in the MATLAB global workspace. For imported global variables, the code generation software uses the initial values to determine only the type.

codegen -config cfg -globals {'myglobal', 1} -args {1}  addglobal_im -report

From the initial value 1, codegen determines that myglobal has type double. codegen declares the imported global variable myglobal. It does not define myglobal or generate code that initializes myglobal. myfile.c provides the code that defines and initializes myglobal.

To view the generated code for myglobal, click the View report link.

myglobal is declared as extern in the Variable Declarations section in addglobal_im_data.h.

/* Variable Declarations */
/* Declaration for custom storage class: ImportedExtern */
extern double myglobal;

In the function addglobal_imptr, assign the 'ImportedExternPointer' storage class to the global variable myglobal.

function y = addglobal_imptr(x)

% Define the global variable.

global myglobal;

% Assign the storage classes.

coder.storageClass('myglobal', 'ImportedExternPointer');
y = myglobal + x;
end

Create a file c:\myfiles\myfile.c that defines and initializes the imported global variable myglobal.

#include <stdio.h>

/* Variable definitions for imported variables */
double v = 1.0; 
double *myglobal = &v;

Create a code configuration object. Configure the code generation parameters to include myfile.c. For output type'lib', or if you generate source code only, you can generate code without providing this file. Otherwise, you must provide this file.

cfg = coder.config('dll','ecoder', true);
cfg.CustomSource = 'myfile.c';
cfg.CustomInclude = 'c:\myfiles';

Generate the code. This example uses the -globals argument to specify the type and initial value of the global variable myglobal. Alternatively, you can define global variables in the MATLAB global workspace. For imported global variables, the code generation software uses the initial values to determine only the type.

codegen -config cfg -globals {'myglobal', 1} -args {1} addglobal_imptr -report

From the initial value 1, codegen determines that myglobal has type double. codegen declares the imported global variable myglobal. It does not define myglobal or generate code that initializes myglobal. myfile.c provides the code that defines and initializes myglobal.

To view the generated code for myglobal, click the View report link.

myglobal is declared as extern in the Variable Declarations section in addglobal_imptr_data.h.

/* Variable Declarations */
/* Declaration for custom storage class: ImportedExternPointer */
extern double *myglobal;

Input Arguments

collapse all

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

Example: 'myglobal'

Data Types: char

Storage class to assign to global_var. storage_class can have 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.

  • 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.

Data Types: char

Limitations

  • 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.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Version History

Introduced in R2015b