| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Stateflow |
| Contents | Index |
| Learn more about Stateflow |
| On this page… |
|---|
Integrating Custom C++ Code for Simulation Integrating Custom C Code for Nonlibrary Charts for Simulation |
Simulate your model in one of these ways:
Click the play button in the toolbar of the Simulink model window or the Stateflow Editor.
Select Simulation > Start in the Simulink model window or the Stateflow 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 Using the Command-Line API to Set Parameters for Simulation and Embeddable Code Generation.
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. |
To integrate custom C++ code for simulation, perform the tasks that follow.
Prepare your custom C++ code for simulation as follows:
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;
}
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.
To include custom C++ code for simulation, you must configure your simulation target and select C++ as the custom code language:
In the Stateflow Editor, select Tools > Open Simulation Target.
In the Configuration Parameters dialog box, select the Simulation Target > Custom Code pane.
Add your custom header file in the Header file subpane. Click Apply.
Add your custom C++ files in the Source files subpane. Click Apply.
In the Configuration Parameters dialog box, select the Real-Time Workshop pane.
Select C++ from the Language menu.
Click OK.
For instructions, see Choosing a Compiler.
For instructions, see Starting Simulation.
To integrate custom C code that applies to nonlibrary charts for simulation, perform the tasks that follow.
Specify custom code options in the simulation target for your model:
In the Stateflow Editor, select Tools > Open Simulation Target.
In the Configuration Parameters dialog box, select the Simulation Target > Custom Code pane.
The custom code options appear.

Specify your custom code in the subpanes.
Follow the guidelines in Specifying 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.
Tip If you want to rebuild the target to include custom code changes, select Tools > Rebuild All in the Stateflow Editor. If you want to build the target only for the parts of a chart that have changed since the previous build, select Tools > Build Diagram in the Stateflow Editor. |
For instructions, see Starting Simulation.
To integrate custom C code that applies only to library charts for simulation, perform the tasks that follow.
Specify custom code options in the simulation target for each library model that contributes a chart to the main model:
In the Stateflow Editor, select Tools > Open Simulation Target.
The Configuration Parameters dialog box appears.

In the Simulation Target pane, select Use local custom code settings (do not inherit from main model).
This action ensures that each library model retains its own custom code settings during simulation.
Specify your custom code in the subpanes.
Follow the guidelines in Specifying Relative Paths for Custom Code.
Note See Task 1: Include Custom C Code in the Simulation Target for descriptions of the custom code options. |
Click OK.
For instructions, see Starting Simulation.
To integrate custom C code that applies to all charts for simulation, perform the tasks that follow.
Specify custom code options in the simulation target for your main model:
In the Stateflow Editor, select Tools > Open Simulation Target.
In the Configuration Parameters dialog box, select the Simulation Target > Custom Code pane.
The custom code options appear.

Specify your custom code in the subpanes.
Follow the guidelines in Specifying Relative Paths for Custom Code.
Note See Task 1: Include Custom C Code in the Simulation Target for descriptions of the custom code options. |
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 Tools > Rebuild All in the Stateflow Editor. If you want to build the target only for the parts of a chart that have changed since the previous build, select Tools > Build Diagram in the Stateflow Editor. |
Configure the simulation target for each library model that contributes a chart to your main model:
In the Stateflow Editor, select Tools > Open Simulation Target.
The Configuration Parameters dialog box appears.
In the Simulation Target pane, clear the Use local custom code settings (do not inherit from main model) check box.
This action ensures that library charts inherit the custom code settings of your main model.
Click OK.
For instructions, see Starting Simulation.
![]() | Choosing a Procedure to Simulate a Model | Speeding Up Simulation | ![]() |

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |