Contenuto principale

Customize Generated Entry-Point C Function Names

Entry-point functions are functions in the generated model code that interface with your external code. This example shows how to customize the names of the generated entry-point functions.

Note: To build an executable from a model with custom entry-point function names, use one of these methods:

  • Select the model configuration parameter Generate an example main program, located in the Code Generation > Templates pane of the Configuration Parameters dialog box.

  • Include a custom main program that uses the custom entry-point function names to invoke the generated entry-point functions.

Open and Explore Model

Open the model EntryPoints.

epModel = "EntryPoints";
open_system(epModel)

Model EntryPoints. The two root-level inports are in a rectangle labeled Input Arguments. The two root-level outports are in a rectangle labeled Output Arguments. The three blocks between the inports and the outports are in a rectangle labeled Step Function Body.

EntryPoints is a nonreusable, single-rate model configured to use the ERT system target file.

You can use one of these configuration options to customize the names of the generated entry-point functions.

The names of functions you do not use any of these options with are created according to the default naming rule for entry-point function names. When the model is configured for component deployment type the default naming rule is modelName_funcName, and you cannot change it. Here, modelName is the name of the model and funcName is the name of the function as defined in Types of Generated Entry-Point Functions. For example, the name of the generated initialization function according to this naming rule is modelName_initialize. In this example, keep the model configured for component deployment type.

When the model is configured for subcomponent deployment type, you can customize the default naming rule. This is demonstrated in the example Customize Default Naming Rule of Generated Entry-Point C Functions in Subcomponent Models.

Specify Function Customization Template for Function Categories

A function customization template is a set of specifications for generating functions. These templates are defined in the Embedded® Coder™ Dictionary of the model, or in a shared dictionary linked to the model. To learn more about templates, see Embedded Coder Dictionary > Function Customization Templates.

When you specify a template for a function category, the code generator uses the Function Naming Rule of the template to generate the names of the entry-point functions in the category, except the names of functions for which you specify individual function customization templates or explicit naming rules. The naming rule of the function customization template is a combination of valid C language characters and the macros $R, $N, $U, $C, and $M. For the definition of each macro, see Embedded Coder Dictionary > Function Naming Rule.

Open the Embedded Coder Dictionary of the model:

  1. Open the Embedded Coder® app.

  2. In the Simulink® Toolstrip, navigate to the C Code tab.

  3. Select Code Interface > Embedded Coder Dictionary (Model).

C Code tab is selected in the Simulink Toolstrip. The Code Interface button list is open and the mouse cursor is selecting Embedded Coder Dictionary (Model).

In the dictionary that opens, create two new function customization templates:

  1. On the left pane, select Function Customization Template.

  2. In the middle pane, click Create to create a new function customization template.

  3. On the right pane, change the value of Name to initTermFcnTemplate.

  4. Specify Function Naming Rule as initTermDictFcn_$N.

  5. Create another new template, execFcnTemplate, and specify its naming rule as execDictFcn_$N.

The macro $N is replaced by the model source name of the generated function, as defined in Types of Generated Entry-Point Functions.

Embedded Coder Dictionary with the Function Customization Template section open. The two function customization templates in the list are initTermFcnTemplate and execFcnTemplate. execFcnTemplate is selected. The Function Naming Rule of the template is execDictFcn_$N.

Specify the new function customization templates as the default templates for the Initialize/Terminate function category and the Execution function category:

Open the Code Mappings editor and navigate to the Function Defaults tab. For the Initialize/Terminate category, select initTermFcnTemplate from the Function Custom Template list. For the Execution category, select execFcnTemplate from the Function Custom Template list.

Code Mappings editor open with the Function Defaults tab selected. The values initTermFcnTemplate and execFcnTemplate are selected from the Function Custom Template list for the Initialize/Terminate and Execution categories, respectively.

The naming rule initTermDictFcn_$N is applied to the initialization and termination functions, and the naming rule execDictFcn_$N is applied to the step function.

In the Simulink Toolstrip, select the C Coder tab. Then, in the Generate Code section, click Generate Code to generate code from the model.

Simulink Toolstrip with the C Code tab selected. The arrow cursor is clicking the Generate Code button.

In the Code pane, navigate to the generated header file EntryPoints.h and examine the declaration of the model entry-point functions.

Code pane showing the declaration of the functions initTermDictFcn_initialize, execDictFcn_step, and initTermDictFcn_terminate in the header file EntryPoints.h.

The function names adhere to the naming rules you specified in the function customization templates.

Specify Function Customization Template for Individual Functions

When you specify a function customization template for an individual entry-point function, its naming rule overrides the naming rule of the function customization template specified for the function category (if there is one). Open the model dictionary again and add another template, loneFcnTemplate. Specify Function Naming Rule for this template as loneDictFcn_$N.

Embedded Coder Dictionary with the Function Customization Template section open. The additional function customization template in the list, loneFcnTemplate, is selected. The Function Naming Rule of the template is loneDictFcn_$N.

Use this new function customization template for the termination function and for the step function. In the Code Mappings editor, navigate to the Functions tab. For the Terminate and Periodic: D1 functions, select loneDictTemplate from the Function Custom Template list.

Code Mappings editor with the Functions tab selected. The value loneDictTemplate is selected from the Function Custom Template list for the Terminate and Periodic: D1 functions.

The values of the Function Preview column are updated for the periodic function and the terminate function according to the naming rule of the loneDicFcn template.

Generate code from the model again, and examine the generated entry-point function declarations in the header file EntryPoints.h.

Code pane showing the declaration of the functions initTermDictFcn_initialize, loneDictFcn_step, and loneDictFcn_terminate in the header file EntryPoints.h.

The initialization function name adheres to the category naming rule while the termination function and the step function names adhere to the naming rule of the individually specified template.

Explicitly Specify Function Naming Rule

Specifying naming rules for individual entry-point functions explicitly overrides the naming rules you specify in function customization templates. In the Code Mappings editor, in the Functions tab, locate the initialization function entry. Double-click the Function Name cell to make it editable. Enter the naming rule explicitRule_$N (and click outside the cell to end the editing of the table cell).

Code Mappings editor with the Functions tab selected. The Function Name value of the Initialize entry is explicitRule_$N.

Alternatively, click the Function Preview cell of the table entry to open the Configure C Initialize Function Interface dialog box. In it, specify the naming rule for the function, explicitRule_$N.

Configure C Initialize Function Interface dialog box. The value of C Initialize Function Name is explicitRule_$N. The value of C function prototype is void explicitRule_initialize(void).

Generate code from the model again, and examine the declaration of the initialization function to see its revised name.

Code pane showing the declaration of the functions explicitRule_initialize, loneDictFcn_step, and loneDictFcn_terminate in the header file EntryPoints.h.

The name of the generated initialization function adheres to the explicit naming rule you specified, explicitRule_$N.

See Also

Topics