Integrate Custom C/C++ Code for Simulation

Start Simulation

Simulate your model by clicking the play button in the toolbar of the editor. See Generated Code Files for Targets You Build for details about the simulation code you generate for your model and the folder structure.

For information on setting simulation options using the command-line API, see Command-Line API to Set Simulation and Code Generation Parameters.

    Note:   You cannot simulate only the Stateflow® blocks in a library model. You must first create a link to the library block in your main model and then simulate the main model.

Integrate Custom C++ Code for Simulation

To integrate custom C++ code for simulation, perform the tasks that follow.

Task 1: Prepare Code Files

Prepare your custom C++ code for simulation as follows:

  1. Add a C function wrapper to your custom code. This wrapper function executes the C++ code that you are including.

    The C function wrapper should have this form:

    int my_c_function_wrapper()
    {
    	.
    	.
    	.
    	//C++ code
    	.
    	.
    	.
    	return result;
    }
    
  2. Create a header file that prototypes the C function wrapper in the previous step.

    The header file should have this form:

    int my_c_function_wrapper();
    

    The value _cplusplus exists if your compiler supports C++ code. The extern "C" wrapper specifies C linkage with no name mangling.

Task 2: Include Custom C++ Source and Header Files for Simulation

To include custom C++ code for simulation, you must configure your simulation target and select C++ as the custom code language:

  1. Open the Model Configuration Parameters dialog box.

  2. In the Model Configuration Parameters dialog box, select the Simulation Target > Custom Code pane.

  3. Add your custom header file in the Header file subpane. Click Apply.

  4. Add your custom C++ files in the Source files subpane. Click Apply.

  5. In the Model Configuration Parameters dialog box, select the Code Generation pane.

  6. Select C++ from the Language menu.

  7. Click OK.

Task 3: Choose a C++ Compiler

For instructions, see Choose a Compiler.

Task 4: Simulate the Model

For instructions, see Start Simulation.

Integrate Custom C Code for Nonlibrary Charts for Simulation

To integrate custom C code that applies to nonlibrary charts for simulation, perform the tasks that follow.

Task 1: Include Custom C Code in the Simulation Target

Specify custom code options in the simulation target for your model:

  1. Open the Model Configuration Parameters dialog box.

  2. In the Model Configuration Parameters dialog box, select the Simulation Target > Custom Code pane.

    The custom code options appear.

  3. Specify your custom code in the subpanes.

    Follow the guidelines in Specify Relative Paths for Custom Code.

    • Source file — Enter code lines to include at the top of a generated source code file. These code lines appear at the top of the generated model.c source file, outside of any function.

      For example, you can include extern int declarations for global variables.

    • Header file — Enter code lines to include at the top of the generated model.h header file that declares custom functions and data in the generated code. These code lines appear at the top of all generated source code files and are visible to all generated code.

        Note:   When you include a custom header file, you must enclose the file name in double quotes. For example, #include ''sample_header.h'' is a valid declaration for a custom header file.

      Since the code you specify in this option appears in multiple source files that link into a single binary, limitations exist on what you can include. For example, do not include a global variable definition such as int x; or a function body such as

      void myfun(void)
      {
      ...
      }
      

      These code lines cause linking errors because their symbol definitions appear multiple times in the source files of the generated code. You can, however, include extern declarations of variables or functions such as extern int x; or extern void myfun(void);.

    • Initialize function — Enter code statements that execute once at the start of simulation. Use this code to invoke functions that allocate memory or perform other initializations of your custom code.

    • Terminate function — Enter code statements that execute at the end of simulation. Use this code to invoke functions that free memory allocated by the custom code or perform other cleanup tasks.

    • Include directories — Enter a space-separated list of the folder paths that contain custom header files that you include either directly (see Header file option) or indirectly in the compiled target.

    • Source files — Enter a list of source files to compile and link into the target. You can separate source files with a comma, a space, or a new line.

    • Libraries — Enter a space-separated list of static libraries that contain custom object code to link into the target.

  4. Click OK.

    Tip   If you want to rebuild the target to include custom code changes, select Code > C/C++ Code > Build Model in the Stateflow Editor.

Task 2: Simulate the Model

For instructions, see Start Simulation.

Integrate Custom C Code for Library Charts for Simulation

To integrate custom C code that applies only to library charts for simulation, perform the tasks that follow.

Task 1: Include Custom C Code in Simulation Targets for Library Models

Specify custom code options in the simulation target for each library model that contributes a chart to the main model:

  1. In the Stateflow Editor, select Simulation > Debug > Simulation Target For MATLAB & Stateflow.

    The Model Configuration Parameters dialog box appears.

  2. In the Simulation Target pane, select Use local custom code settings (do not inherit from main model).

    This step ensures that each library model retains its own custom code settings during simulation.

  3. Specify your custom code in the subpanes.

    Follow the guidelines in Specify Relative Paths for Custom Code.

  4. Click OK.

Task 2: Simulate the Model

For instructions, see Start Simulation.

Integrate Custom C Code for All Charts for Simulation

To integrate custom C code that applies to all charts for simulation, perform the tasks that follow.

Task 1: Include Custom C Code in the Simulation Target for the Main Model

Specify custom code options in the simulation target for your main model:

  1. Open the Model Configuration Parameters dialog box.

  2. In the Model Configuration Parameters dialog box, select the Simulation Target > Custom Code pane.

    The custom code options appear.

  3. Specify your custom code in the subpanes.

    Follow the guidelines in Specify Relative Paths for Custom Code.

  4. Click OK.

    By default, settings in the Simulation Target > Custom Code pane for the main model apply to all charts contributed by library models.

    Tip   If you want to rebuild the target to include custom code changes, select Code > C/C++ Code > Build Model in the Stateflow Editor.

Task 2: Ensure That Custom C Code for the Main Model Applies to Library Charts

Configure the simulation target for each library model that contributes a chart to your main model:

  1. In the Stateflow Editor, select Simulation > Debug > Simulation Target For MATLAB & Stateflow.

  2. In the Simulation Target pane, clear the Use local custom code settings (do not inherit from main model) check box.

    This step ensures that library charts inherit the custom code settings of your main model.

  3. Click OK.

Task 3: Simulate the Model

For instructions, see Start Simulation.

Was this topic helpful?