Main Content

coder.reservedName

Generate code that does not use specified identifier names

Since R2020b

Description

coder.reservedName(name1,name2,...,nameN) 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.

example

Examples

collapse all

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 using coder.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 identifier g in the generated code.

Define the MATLAB function callfoo that declares a global variable g, 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 file foo.h:

int foo(int x);

Define the function foo in the C source file foo.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.

extern int b_g;

Input Arguments

collapse all

Identifier names that generated code does not use, specified as character vectors.

Example: 'myname1','myname2','myname3'

Data Types: char

Tips

  • The following code generation setting provides the same functionality as coder.reservedName. To specify identifier names that the generated code does not use:

    • In a code configuration object, set the ReservedNameArray property

    • Alternatively, in the MATLAB Coder™ app, on the Code Appearance tab, set the Reserved names parameter

Extended Capabilities

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

GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.

Version History

Introduced in R2020b