Main Content

Code Mappings – C++ Editor

Configure how model elements and functions appear in generated C++ code

Description

The Code Mappings editor is a graphical interface used to configure how Simulink® model data elements and functions appear in generated C++ code.

To configure Simulink model data elements and functions for C++ code generation, use the tables in the Code Mappings editor:

  • Data

  • Functions

Simulink model showing the open Code Mappings - C++ Editor

Open the Code Mappings – C++ Editor

Open the Embedded Coder® app. Verify the Output is set to Embedded C++ Code. On the C++ Code tab, click the Code Interface menu and select Code Mappings.

Examples

Configure Data Elements

You can use the Code Mappings editor to customize the data visibility and method access of model elements in the generated C++ class interface. This example uses the model rtwdemo_roll to show how to configure the data visibility and method access for the inports in this model.

Set Up the Environment
  1. Open model rtwdemo_roll.

  2. Open the Embedded Coder app. In the Apps gallery, click Embedded Coder.

  3. Set the language to C++. On the C++ Code tab, click Output and select Embedded C++ Code.

  4. Open the Code Mappings editor. From the tab, click Code Interface and select Code Mappings.

Customize the Data Visibility and Method Access of Data Elements
  1. In the Code Mappings editor, click the Data tab.

  2. Customize the data visibility. For the Inports category, in the Data Visibility column, select public.

  3. Customize the method access. For the Inports category, in the Member Access Method column, select method.

Generate and Verify Code
  1. Generate code.

  2. Verify the generated C++ code for the inports. In the Code view, open rtwdemo_roll.cpp file and search for the inports in the model.

Configure Functions

You can use the Code Mappings editor to customize the names of entry-point functions and the names and arguments of base-rate periodic or Simulink Functions. This examples uses the model rtwdemo_roll to show how to configure the name and arguments of a base-rate periodic function.

Set Up the Environment
  1. Open model rtwdemo_roll.

  2. Open the Embedded Coder app. In the Apps gallery, click Embedded Coder.

  3. Set the language to C++. On the C++ Code tan, click Output and select Embedded C++ Code.

  4. Open the Code Mappings editor. From the tab, click Code Interface and select Code Mappings.

Customize Function Name and Arguments
  1. In the Code Mappings editor, click the Functions tab.

  2. Customize the name of the periodic function. In the Function Name column, enter the name roll_run.

  3. Customize the arguments of the periodic function. In the Function Preview column, click the prototype hyperlink. A configuration dialog box opens.

  4. In the dialog box, select Configure arguments for Step function prototype and click Get Default. A table that displays the arguments opens.

  5. Customize the arguments:

    • From the C++ return argument drop-down list, select Ail_Cmd.

    • For each port, in the C++ Identifier Name field, remove the arg_ prefix from their default names.

    • For the HDG_Mode inport, from the C++ Type Qualifier drop-down list, select Pointer. In the C++ Identifier Name field change the name to HDG_Mode_Ptr

  6. Click Apply. Visually verify that the function prototype reflects the changes. Click OK to exit.

Generate and Verify Code
  1. Generate code.

  2. Verify the updated method names and arguments in the generated C++ code. In the Code view, in the open rtwdemo_roll.cpp file, search for the base-rate periodic function, roll_run.

Parameters

Data

Each category describes a type of Simulink model data element. The data visibility and method access set for a category applies to the data elements in that category for the model.

Model Element CategoryDescription
InportsRoot-level data input ports of a model, such as Inport and In Bus Element blocks.
OutportsRoot-level data output ports of a model, such as Outport and Out Bus Element blocks.
Model parameter argumentsWorkspace variables that appear as per instance (nonstatic) class data members.
Model parametersWorkspace variables that are shared across instances of the model class and are generated as static class data members.
Signals, states, and internal dataData elements that are internal to the model, such as block output signals, discrete block states, data stores, and zero-crossing signals.

The data visibility determines if data elements appear in generated code as public or private. For model parameter arguments, this parameter can also specify that data elements are generated outside the class and passed as individual arguments.

The access determines how the generated code provides access to the class member data.

Functions

The functions in a model that generate entry-point methods in a C++ class interface. The functions include:

  • Initialize Functions

  • Terminate Functions

  • Periodic Functions

  • Partition Functions

  • Exported Functions

  • Reset Functions

  • Simulink Functions

Name for the generated class method.

Preview of the entry-point method prototype. To customize the prototype, click the preview hyperlink and configure the method in the opened dialog box.

Introduced in R2021a