Code Generation Template (CGT) Files
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.
Default CGT file
The code generation software provides a default CGT file,
.
Base your custom CGT files on the default file.matlabroot
/toolbox/rtw/targets/ecoder/ert_code_template.cgt
CGT File Structure
A CGT file consists of one required section and four optional sections:
Code Insertion Section
(Required) This section contains tokens that define an ordered partitioning of the
generated code into a number of sections (such as Includes
and
Defines
sections). Tokens have the form of:
%<SectionName>
For example,
%<Includes>
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.
File Banner Section
(Optional) This section contains comments and tokens you use in generating a custom file banner.
Function Banner Section
(Optional) This section contains comments and tokens for use in generating a custom function banner.
Shared Utility Function Banner Section
(Optional) This section contains comments and tokens for use in generating a custom shared utility function banner.
File Trailer Section
(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.
Built-In Tokens and Sections
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.
Note
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 | Description |
---|---|
|
|
|
|
|
|
| Enumerated types section |
| Data definitions (for example, |
| Data declarations (for example, |
| C or C++ functions |
Subsections
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.
Note
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
Section | Subsections | Subsection Description |
---|---|---|
| N/A | |
| N/A | |
|
| Intrinsic |
|
| Primitive |
|
| You can place any type of code in this section, including code that has dependencies on the previous sections. |
|
|
|
| N/A | |
| N/A | |
| N/A | |
| C or C++ functions | |
|
|
|
|
|
|
|
| Documentation (comment) section |
|
| You can place any code in this section. |