coder.storageClass
Assign storage class to global variable
Description
coder.storageClass(
assigns
the storage class global_name
, 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.
Examples
Export Global Variables
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 theExported data define
section inaddglobals_ex.h
./* Exported data define */ /* Definition for custom storage class: ExportedDefine */ #define myglobaltwo 2.0
myglobalone
is defined in theVariable Definitions
section inaddglobals_ex.c
./* Variable Definitions */ /* Definition for custom storage class: ExportedGlobal */ double myglobalone;
myglobalone
is declared asextern
in theVariable Declarations
section inaddglobals_ex.h
./* Variable Declarations */ /* Declaration for custom storage class: ExportedGlobal */ extern double myglobalone;
myglobalone
is initialized inaddglobals_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; }
Import Global Variable
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;
Import External Pointer
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
global_name
— Name of global variable
character vector
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
— Name of storage class
'ExportedGlobal'
| 'ExportedDefine'
| 'ImportedExtern'
| 'ImportedExternPointer'
Storage class to assign to global_var
. storage_class
can
have one of the following values.
Storage Class | Description |
---|---|
'ExportedGlobal' |
|
'ExportedDefine' | Declares the variable with a |
'ImportedExtern' | Declares the variable as an |
'ImportedExternPointer' | Declares the variable as an |
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
. When the global variable does not have a storage class, the variable is declared in the fileentry_point_name
.h
.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
Comando de MATLAB
Ha hecho clic en un enlace que corresponde a este comando de MATLAB:
Ejecute el comando introduciéndolo en la ventana de comandos de MATLAB. Los navegadores web no admiten comandos de MATLAB.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)