Control Declarations and Definitions of Global Variables in Code Generated from MATLAB Code
This example uses storage classes to control the declarations and definitions of global variables in C/C++ code generated from MATLAB® code. Using storage classes helps you to interface generated code with external code.
This example requires an Embedded Coder® license.
Write a function addglobals
that
adds four global variables. Declare the global variables in the function.
function y = addglobals %#codegen % Define the global variables. global u; global v; global x; global z; % Assign the storage classes. coder.storageClass('u','ExportedGlobal'); coder.storageClass('v','ImportedExtern'); coder.storageClass('x','ImportedExternPointer'); coder.storageClass('z','ExportedDefine'); y = u + v + x + z; end
Create a file c:\myfiles\myfile.c
that
defines and initializes the imported global variables u
and v
.
#include <stdio.h> /* Variable definitions for imported variables */ double v = 1.0; double *x = &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 types and initial values of the global variables u
, v
, x
,
and z
. Alternatively, you can define global variables
in the MATLAB global workspace. For the imported global variables v
and x
,
the code generator uses the initial values only to determine the type.
codegen -config cfg -globals {'u', 1, 'v', 2, 'x', 3, 'z', 4} addglobals -report
From the initial values 1
, 2
, 3
,
and 4
codegen
determines that u
, v
, x
and z
have
type double
. codegen
defines
and declares the exported global variables u
and z
.
It generates code that initializes u
to 1.0
and z
to 4.0
. codegen
declares
the imported global variables v
and x
.
It does not define these variables or generate code that initializes
them. myfile.c
provides the code that defines and
initializes v
and x
.
To view the code generated for the global variables, open the report. Click the View report link.
View the definition for the exported global z
in
the Exported data define
section in addglobals.h
.
/* Definition for custom storage class: ExportedDefine */ #define z 4.0
View the definition and declaration for the exported global u
.
u
is defined in theVariable Definitions
section inaddglobals.c
./* Variable Definitions */ /* Definition for custom storage class: ExportedGlobal */ double u;
u
is declared asextern
in theVariable Declarations
section inaddglobals.h
./* Variable Declarations */ /* Declaration for custom storage class: ExportedGlobal */ extern double u;
u
is initialized inaddglobals_initialize.c
./* Include Files */ #include "addglobals_initialize.h" #include "addglobals.h" #include "addglobals_data.h" /* Function Definitions */ /* * Arguments : void * Return Type : void */ void addglobals_initialize(void) { u = 1.0; isInitialized_addglobals = true; }
View the definition and declaration for the imported external
global v
and the imported external global pointer x
.
v
and x
are declared as extern
in
the Variable Declarations
section in addglobals_data.h
.
/* Variable Declarations */ /* Declaration for custom storage class: ImportedExtern */ extern double v; /* Declaration for custom storage class: ImportedExternPointer */ extern double *x;