coder.reservedName - Generate code that does not use specified identifier names - MATLAB (original) (raw)
Main Content
Generate code that does not use specified identifier names
Since R2020b
Syntax
Description
coder.reservedName([name1,name2,...,nameN](#mw%5F0bd359ca-054a-45c5-bacf-bfbf266f62be))
reserves the identifiers name1,name2,...,nameN
for use in your custom C/C++ code to integrate with your generated code. If you place this directive in your MATLABĀ® code intended for code generation, the generated code does not contain the identifiers name1,name2,...,nameN
, thereby preventing name collision with your custom code.
Examples
Suppose that the identifier g
denotes both a global variable in your MATLAB code and a local variable in your custom C code, which you call by usingcoder.ceval. By default, the generated code uses the same identifier that MATLAB uses for the global variable. But, in this case, such usage in the generated code can cause a name collision with the local variable g
in the custom code. This example shows how to instruct the code generator to not use the identifierg
in the generated code.
Define the MATLAB function callfoo
that declares a global variableg
, calls an external C function foo
, and returns the sum of g
and the value that foo
returns. Insert the coder.reservedName('g')
directive to instruct the code generator to not use the identifier g
in the generated code.
function u = callfoo(n) %#codegen % Reserve 'g' for use in the C function foo coder.reservedName('g');
global g u = int32(0);
coder.updateBuildInfo('addSourceFiles','foo.c'); coder.cinclude('foo.h');
u = coder.ceval('foo', n); u = u + g; end
Declare the function foo
in the C header filefoo.h
:
Define the function foo
in the C source filefoo.c
. This function accepts an integer input and returns the factorial of that integer.
#include <stdio.h>
#include <stdlib.h>
#include "foo.h"
int foo(int x)
{
int count;
int g = 1;
for (count = 1;count <= x;count++)
{
g = g*count;
}
return (g);
}
Generate a static library for callfoo
. Specify the input as a scalar 32-bit integer.
codegen -config:lib -global {'g', int32(2)} callfoo -args {int32(0)} -report
Inspect the generated header file callfoo_data.h
. The name of the global variable has been changed to b_g
.
Input Arguments
Identifier names that generated code does not use, specified as character vectors.
Example: 'myname1','myname2','myname3'
Data Types: char
Tips
- The Reserved names parameter provides the same functionality as
coder.reservedName
.
Extended Capabilities
Version History
Introduced in R2020b