Code Generation Template Files for MATLAB Code - MATLAB & Simulink (original) (raw)

A code generation template (CGT) file defines the sections in generated code that you can customize using comments and tokens. Using a code generation template (CGT) file for the generation of C and C++ code from MATLAB® code, you can specify custom file banners and function banners for generated code. File banners are comment sections in the header and trailer sections of a generated file. Function banners are comment sections for each function in the generated code. You can also customize comments before code sections. Use these banners to:

For information on creating, customizing, and using a CGT file, see Generate Custom File and Function Banners for C/C++ Code.

Default CGT File

You can base your custom template on the default CGT file, matlabcoder_default_template.cgt, in the _`matlabroot`_/toolbox/coder/matlabcoder/templates/ folder.

Note

If you choose not to customize banners for your generated code, the default template is used for code generation.

CGT File Structure

A CGT file consists of 13 optional sections.

File Banner Section

Contains comments and tokens for use in generating a custom file banner.

Function Banner Section

Contains comments and tokens for use in generating a custom function banner.

Shared Utility Function Banner

Contains comments and tokens for use in generating custom banners for shared utility functions.

File Trailer Section

Contains comments for use in generating a custom trailer banner.

Include Files Banner

Contains comments for use in generating a custom banner for the include files section.

Type Definitions

Contains comments for use in generating a custom banner for the type definitions section.

Named Constants

Contains comments for use in generating a custom banner for the named constants section.

Variable Declarations

Contains comments for use in generating a custom banner for the variable declarations section.

Variable Definitions

Contains comments for use in generating a custom banner for the variable definitions section.

Function Declarations

Contains comments for use in generating a custom banner for the function declarations section.

Function Definitions

Contains comments for use in generating a custom banner for the function definitions section.

Custom Source Code

Contains comments for use in generating a custom banner for the custom source code section.

Custom Header Code

Contains comments for use in generating a custom banner for the custom header code section.

Components of the CGT File Sections

Each CGT file section is defined by open and close tags.

You can customize your banners by including tokens and comments between the open and close tags for each section. Tokens are replaced with values in the generated code. The following rules apply to tokens in your CGT file:

Note

In the contents of your banner, C comment indicators, '/*' or '*/', can introduce an error in the generated code.

An open tag includes tag attributes. Enclose the value of the attribute in double quotes. The attributes available for an open tag are:

The open tag syntax is:

<OpenTag style = “style_value” width = “num_width”>

There are five options for the banner style. The CommentStyle and TargetLang configuration object properties determine the use of C or C++ comment style. The built-in style options for the style attribute are:

File Banner

This section contains comments and tokens for use in generating a custom file banner that precedes the generated C and C++ code. If you omit the file banner section from the CGT file, the code generator does not generate a file banner in the generated code. The file banner section provided in the default CGT file is:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Custom File Banner section (optional) %% Customize File banners by using either custom tokens or the following %% predefined tokens: %% %, %, % %% %, %, % %% %% You can also use "custom tokens" in all of the sections below. See the %% documentation center for more details. %% File: %

MATLAB Coder version : % C/C++ source code generated on : %

Summary of Tokens for File Banner Generation

FileName Name of the generated file (for example, "kalman.c")
SourceGeneratedOn Timestamp of generated file
MATLABCoderVersion Version of MATLAB Coder™
EmbeddedCoderVersion Version of Embedded Coder®
HardwareSelection Selected target
OutputType Type of output (for example, lib, exe, or dll)

Function Banner

This section contains comments and tokens for use in generating a custom function banner that precedes a generated C or C++ function. If you omit the function banner section from the CGT file, the code generator does not generate function banners. The function banner section provided in the default CGT file is:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Custom Function Banner section (optional) %% Customize function banners by using the following predefined tokens: %% %, %, % %% %, % %% % % Arguments : % Return Type : %

Summary of Tokens for Function Banner Generation

FunctionName Name of function
FunctionDescription Short abstract about the function If the configuration parameter MATLABFcnDesc istrue, the function description appears in the function banner. Otherwise, the code generator treats the function description as user comments.
FunctionPrototype MATLAB function prototype The function prototype appears in the function banner only if theMATLABSourceComments configuration parameter istrue.
Arguments List of function arguments
ReturnType Return type of function

Shared Utility Banner

This section contains comments and tokens for use in generating a custom shared utility function banner that precedes a generated C or C++ shared utility function. If you omit the shared utility function banner section from the CGT file, the code generator does not generate shared utility function banners. The shared utility function banner section provided in the default CGT file is:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Custom Shared Utility Function Banner section (optional) %% Customize shared utility function banners by using the following %% predefined tokens: %% %, % %% %, % %% Arguments : % Return Type : %

Summary of Tokens for Shared Utility Function Banner Generation

FunctionName Name of function
FunctionDescription Short abstract about the function
Arguments List of function arguments
ReturnType Return type of function

File Trailer

The file trailer section contains comments for generating a custom file trailer that follows the generated C or C++ code. If you omit the file trailer section from the CGT file, the code generator does not generate a file trailer. The file trailer section provided in the default CGT file is:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Custom file trailer section (optional) %% You can use any of the predefined tokens used for File Banner %% File trailer for %

[EOF]

Tokens for the file banner are available for the file trailer. See Summary of Tokens for File Banner Generation.

Include Files Banner

The include files banner section contains comments for generating a custom banner that precedes the include files section in the generated code. If you omit the include files banner section from the CGT file, the code generator does not generate a banner for this section. The include files banner section provided in the default CGT file is:

Include Files

Type Definitions Banner

The type definitions banner section contains comments for generating a custom banner that precedes the type definitions section in the generated code. If you omit the type definitions banner section from the CGT file, the code generator does not generate a banner for this section. The type definitions banner section provided in the default CGT file is:

Type Definitions

Named Constants Banner

The named constants banner section contains comments for generating a custom banner that precedes the named constants section in the generated code. If you omit the named constants banner section from the CGT file, the code generator does not generate a banner for this section. The named constants banner section provided in the default CGT file is:

Named Constants

Variable Declarations

The variable declarations banner section contains comments for generating a custom banner that precedes the variable declarations section in the generated code. If you omit the variable declarations banner section from the CGT file, the code generator does not generate a banner for this section. The variable declarations banner section provided in the default CGT file is:

Variable Declarations

Variable Definitions

The variable definitions banner section contains comments for generating a custom banner that precedes the variable definitions section in the generated code. If you omit the variable definitions banner section from the CGT file, the code generator does not generate a banner for this section. The variable definitions banner section provided in the default CGT file is:

Variable Definitions

Function Declarations

The function declarations banner section contains comments for generating a custom banner that precedes the function declarations section in the generated code. If you omit the function declarations banner section from the CGT file, the code generator does not generate a banner for this section. The function declarations banner section provided in the default CGT file is:

Function Declarations

Function Definitions

The function definitions banner section contains comments for generating a custom banner that precedes the function definitions section in the generated code. If you omit the function definitions banner section from the CGT file, the code generator does not generate a banner for this section. The function definitions banner section provided in the default CGT file is:

Function Definitions

Custom Source Code

The custom source code banner section contains comments for generating a custom banner that precedes the custom source code section in the generated code. If you omit the custom source code banner section from the CGT file, the code generator does not generate a banner for this section. The custom source code banner section provided in the default CGT file is:

Custom Source Code

Custom Header Code

The custom header code banner section contains comments for generating a custom banner that precedes the custom header code section in the generated code. If you omit the custom header code banner section from the CGT file, the code generator does not generate a banner for this section. The custom header code banner section provided in the default CGT file is:

Custom Header Code

See Also

coder.MATLABCodeTemplate