Access Custom C Code in Nonlibrary Charts

You can integrate custom code written in C or C++ with Stateflow® charts in Simulink® models. By sharing data and functions between your custom code and your Stateflow chart, you can augment the capabilities of Stateflow and leverage the software to take advantage of your preexisting code. For more information, see Reuse Custom Code in Stateflow Charts.

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 pane.

    The custom code options appear.

  3. Specify your custom code in the subpanes.

    Follow the guidelines in Specify Relative Paths to Your 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.

Task 2: Simulate the Model

Simulate your model by clicking the play button in the toolbar of the editor.

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

Related Topics