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 #define and #include 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

      Because the code you specify in this 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).

    • 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).

    • 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