Code Generation Template (CGT) files define the top-level organization and formatting of generated source code and header files. CGT files have the following applications:
Generation of custom banners (comments sections) in code files. See Generate Custom File and Function Banners.
Generation of custom code using a CFP template requires a CGT file. To use CFP templates, you must understand the CGT file structure. In many cases, however, you can use the default CGT file without modifying it.
The code generation software provides a default CGT file,
Base your custom CGT files on the default file.
A CGT file consists of one required section and four optional sections:
(Required) This section contains tokens that define an ordered partitioning of the
generated code into a number of sections (such as
Defines sections). Tokens have the form of:
The code generation software defines a minimal set of required tokens. These tokens generate C or C++ source or header code. They are built-in tokens (see Built-In Tokens and Sections). You can also define custom tokens and custom sections.
Each token functions as a placeholder for a corresponding section of generated code. The ordering of the tokens defines the order in which the corresponding sections appear in the generated code. If you do not include a token, then the corresponding section is not generated. To generate code into a given section, explicitly call the code template API from a CFP template, as described in Custom File Processing (CFP) Templates.
The CGT tokens define the high-level organization of generated code. Using the code template API, you can partition each code section into named subsections, as described in Subsections.
In the code insertion section, you can also insert C or C++ comments between tokens. Such comments emit directly into the generated code.
(Optional) This section contains comments and tokens you use in generating a custom file banner.
(Optional) This section contains comments and tokens for use in generating a custom function banner.
(Optional) This section contains comments and tokens for use in generating a custom shared utility function banner.
(Optional) This section contains comments for use in generating a custom trailer banner.
For more information on these sections, see Generate Custom File and Function Banners.
The following code extract shows the required code insertion section of the default CGT file with the required built-in tokens.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Code insertion section (required) %% These are required tokens. You can insert comments and other tokens in %% between them, but do not change their order or remove them. %% %<Includes> %<Defines> %<Types> %<Enums> %<Definitions> %<Declarations> %<Functions>
Note the following requirements for customizing a CGT file:
Do not remove required built-in tokens.
Built-in tokens must appear in the order shown because each successive section has dependencies on previous sections.
Only one token per line.
Do not repeat tokens.
You can add custom tokens and comments to the code insertion section as long as you do not violate the previous requirements.
If you modify a CGT file and then rebuild your model, the code generation process does not force a top model build. To regenerate the code, see Force Regeneration of Top Model Code.
The following table summarizes the built-in tokens and corresponding section names, and describes the code sections.
Built-In CGT Tokens and Corresponding Code Sections
Token and Section Name
Enumerated types section
Data definitions (for example,
Data declarations (for example,
C or C++ functions
You can define one or more named subsections for any section. Some of the built-in sections have predefined subsections summarized in table Subsections Defined for Built-In Sections.
Sections and subsections emit to the source or header file in the order listed in the CGT file.
Using the custom section feature, you can define additional sections. See Generate a Custom Section.
Subsections Defined for Built-In Sections
You can place any type of code in this section, including code that has dependencies on the previous sections.
C or C++ functions
Documentation (comment) section
You can place any code in this section.