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 Configuration Parameters dialog box.

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

    The Simulation Custom Code pane of the 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 add custom code settings for simulation that are unique to your library model, in the Code information tab, specify these parameters:

    • Include headers — 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 #include and #define statements. When you include a custom header file, you must enclose the file name in double quotes. For more information, see Include headers (Simulink).

      Note

      • The code you specify in this parameter can include extern declarations of variables or functions, such as extern int x or extern void myfun(void). However, global variable or function definitions such as int x or void myfun(void) cause linking errors because the definitions appear multiple times in the source files of the generated code.

      The Simulation Target Custom Code "Include headers" tab has three lines of code: #include "util.h", #include "table.h", and #include "extra functions.h"

    • Include directories — Enter a list of the folder paths that contain custom header, source, and library files that you include either directly in the configuration parameters or indirectly in the compiled target. You can separate entries with newlines, spaces, or semicolons. For more information, see Include directories (Simulink).

      The Simulation Target Custom Code "Include directories" tab shows five lines: src, includes, includes/utilities, lib, "lib/additional libraries"

    • Source files — Enter a list of source files to compile and link into the target separated by newlines, spaces, or semicolons. For more information, see Source files (Simulink).

      The Simulation Target Custom Code "Source files" tab shows three lines: util.c, table.c, and "extra functions.c"

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

      The Simulation Target Custom Code "Libraries" tab shows two lines: addons.dll, and library.dll

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

    • Compiler flags — Enter additional compiler flags to be added to the compiler command line when your custom code is compiled. For more information, see Compiler flags (Simulink).

    • Linker flags — Enter additional linker flags to be added to the linker command line when your custom code is linked. For more information, see Linker flags (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 Configuration Parameters dialog box.

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

  2. Open the Code Generation Custom Code pane.

    The Code Generation Custom Code pane of the 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. In the Code Information tab, specify custom code settings for code generation. For descriptions of the parameters in this tab, 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