Main Content

Configure Custom Code in Library Models

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 take advantage of your preexisting code. For more information, see Reuse Custom Code in Stateflow Charts.

Configure Custom Code Settings for Simulation

To configure your library model to access custom code during simulation, use the Simulation Custom Code pane of the Model Configuration Parameters dialog box.

  1. In the Modeling tab, under Design, select Simulation Custom Code.

    The Simulation Custom Code pane of the Model Configuration Parameters dialog box, showing the default settings for a library model.

  2. A library model can inherit the custom code settings from the main model or use local custom code settings for simulation.

    • To inherit the custom code settings from the main model, clear Use local custom code settings (do not inherit from main model).

    • To use local custom code settings for simulation, select Use local custom code settings (do not inherit from main model).

    For more information, see Use local custom code settings (do not inherit from main model) (Simulink).

  3. To specify local custom code settings for simulation that are unique to your library model, use the Insert custom C code in generated and Additional build information subpanes.

    • Source file — Enter the code to include at the top of a generated source code file. This code appears at the top of the generated model.c source file, outside of any function. For example, use this parameter to include extern int declarations for global variables. For more information, see Source file (Simulink).

    • Header file — Enter the code to include at the top of the generated model.h header file, which declares custom functions and data in the generated code. The code appears at the top of all generated source code files and is visible to all generated code. For example, use this parameter to enter #define and #include statements. When you include a custom header file, you must enclose the file name in double quotes. For more information, see Header file (Simulink).

      Note

      Because the code you specify in the Header file parameter appears in multiple source files that link into a single binary, you cannot include a global variable definition such as int x or a function with a signature of void myfun(void). These statements 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 that executes once at the start of simulation. Use this code to invoke functions that allocate memory or initialize your custom code. For more information, see Initialize function (Simulink).

    • Terminate function — Enter code that executes at the end of simulation. Use this code to invoke functions that free memory allocated by the custom code or perform other cleanup tasks. For more information, see Terminate function (Simulink).

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

    • Source files — Enter a list of source files to compile and link into the target. You can separate source files with a comma, space, or new line. For more information, see Source files (Simulink).

    • Libraries — Enter a space-separated list of static libraries that contain custom object code to link into the target. For more information, see Libraries (Simulink).

    • Defines — Enter a space-separated list of preprocessor macro definitions to add to the generated code. For more information, see Defines (Simulink).

    These settings apply only when you select Use local custom code settings (do not inherit from main model).

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.

Configure Custom Code Settings for Code Generation

To configure your library model to access custom code during code generation, use the Code Generation Custom Code pane of the Model Configuration Parameters dialog box.

  1. In the Modeling tab, under Design, select Simulation Custom Code.

  2. Select the Code Generation Custom Code pane.

    The Code Generation Custom Code pane of the Model Configuration Parameters dialog box, showing the default settings for a library model.

  3. When generating code, a library model can use the same custom code settings that it uses for simulation or use unique custom code settings.

    • To use the same custom code settings used for simulation, select Use the same custom code settings as Simulation Target. Specify the custom code settings in the Simulation Custom Code pane as described in Configure Custom Code Settings for Simulation.

    • To use unique custom code settings, clear Use the same custom code settings as Simulation Target. Specify custom code settings for code generation in the Insert custom C code in generated and Additional build information subpanes. For descriptions of the parameters in these subpanes, see Configure Custom Code Settings for Simulation.

    For more information, see Use the same custom code settings as Simulation Target (Simulink Coder) and Integrate External Code by Using Model Configuration Parameters (Simulink Coder).

Related Topics