Main Content

Generate C++ Code by Using Embedded Coder Quick Start

Prepare the CppClassWorkflowKeyIgnition model for embedded code generation by using the Embedded Coder® Quick Start tool. The Quick Start tool chooses fundamental code generation settings based on your goals and application.

For this step of the tutorial, you generate code for the CppClassWorkflowKeyIgnition model, and then inspect the generated files. The CppClassWorkflowKeyIgnition model represents an ignition system for a vehicle.

Generate C++ Code by Using Quick Start Tool

  1. Open the model CppClassWorkflowKeyIgnition.


    View of the CppClassWorkflowKeyIgnition model

  2. Save a copy of the model to a writable location on the MATLAB search path.

  3. If the C++ Code tab is not already open, on the Apps tab, in the Apps gallery, under Code Generation, click Embedded Coder.

  4. On the C++ Code tab, click Settings and select C/C++ Code generation settings.

    The Configuration Parameters dialog box opens.

  5. On the Configuration Parameters dialog box, open the Code Generation pane, and under Target selection, verify the parameter Language is set to C++. Click OK to close the dialog box.

  6. On the C++ Code tab, click Quick Start.

    View of the System display in the Embedded Coder Quick Start tool for the CppClassWorkflowKeyIgnition model

  7. Advance through the Quick Start tool by clicking Next in each step.

    Each step asks questions about the code that you want to generate. For this tutorial, use the defaults. The tool validates your selections against the model and presents the parameter changes required to generate code.

  8. In the Generate Code step, apply the proposed changes and generate code from CppClassWorkflowKeyIgnition by clicking Next.

  9. Click Finish, then return to the C++ Code tab.

View of the CppClassWorkflowKeyIgnition model in Simulink. The toolstrip is at the top. The Simulink model is in the middle. The Code view pane is on the right.

Inspect Generated C++ Code

The code generator converts the CppClassWorkflowKeyIgnition model into a C++ class, which you access from your application code. The model data elements appear as class members. The Simulink® functions appear as class methods.

The CppClassWorkflowKeyIgnition model incorporates elements of rate-based and export-function modeling. For rate-based modeling, the entry-point class methods, which you call from your application code, include an initialization method, an execution method, a terminate method, and, optionally, a reset method. To integrate with external code or interface requirements, you can customize the generated class interface.

For this tutorial, the code generated by using the Quick Start tool uses default settings for the class elements. The default name for the C++ class is the model name CppClassWorkflowKeyIgnition.

To inspect the C++ class information generated for the model:

  1. On the right side of the Simulink Editor window, in the Code view pane, locate the search bar.

  2. In the search bar, type the model class name CppClassWorkflowKeyIgnition to find each instance of the class name across the generated code, and then click the highlighted search suggestion.

    Use the search functionality in the Code view pane

  3. Use the arrows on the right under the search bar to step through each instance, including the class definition in CppClassWorkflowKeyIgnition.h and the class instantiation in ert_main.cpp.

    You can also see the number of search results in each file from the file menu in the upper-left corner.

    Use the search functionality in the Code view pane to view search hits in each file

  4. Review the data and function code mappings.

    In the Simulink Editor window, click Code Interface, and select Code Mappings to open the Code Mappings editor.

    View of the CppClassWorkflowKeyIgnition model in Simulink. The toolstrip is at the top. The Simulink model is in the middle. The Code Mappings pane is at the bottom. The Data tab in the Code Mappings pane is selected. The Code view pane is on the right.

  5. Click the Data tab to view class member visibility and access methods.

    Simulink data elements are grouped into categories of modeling elements:

    • Inports: Root-level data input ports of a model.

    • Outports: Root-level data output ports of a model.

    • Model parameter arguments: Workspace variables that appear as instance (nonstatic) class data members.

    • Model parameters: Workspace variables that are shared across instances of the model class that are generated as static class data members.

    • Signals, states, and internal data: Data elements that are internal to a model, such as block output signals, discrete block states, data stores, and zero-crossing signals.

  6. Click the Functions tab to view class methods.

    The generated class methods are entry-point methods and are locations in code where a transfer of program control occurs.

    View of the Code Mappings - Component Interface pane for the CppClassWorkflowKeyIgnition model in Simulink. The Functions tab in the Code Mappings pane is selected

    The default name for the initialize class method is initialize, the execution (step) method is step0, and the terminate method is terminate.

  7. Repeat the search steps to locate class methods initialize, step0, and terminate in the generated code.

Next, configure a customized class interface for code generation and review the generated code.