Documentation Center

  • Trial Software
  • Product Updates

Integrate Custom C/C++ Code for Code Generation

Generate Code

Generate embeddable code for your model in one of these ways:

  • Use the keyboard shortcut Ctrl-B or Command-B.

  • Click Build in the Code Generation pane of the Model Configuration Parameters dialog box.

See Generated Code Files for Targets You Build for details about the embeddable code you generate for your model and the folder structure.

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

    Note:   You cannot generate embeddable code only for the Stateflow® blocks in a library model. You must first create a link to the library block in your main model and then generate code for the main model.

Integrate Custom C++ Code for Code Generation

To integrate custom C++ code for embeddable code generation, perform the tasks that follow.

Task 1: Prepare Code Files

Prepare your custom C++ code for code generation.

  1. 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;
    }
    
  2. 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.

Task 2: Include Custom C++ Source and Header Files for Code Generation

To include custom C++ code for Simulink® Coder™ code generation, perform these steps:

  1. Open the Model Configuration Parameters dialog box.

  2. In the Model Configuration Parameters dialog box, select the Code Generation pane.

  3. Select C++ from the Language menu. Click Apply.

  4. Select the Code Generation > Custom Code pane.

  5. Add your custom header file in the Header file subpane. Click Apply.

  6. Add your custom C++ files in the Source files subpane.

  7. Click OK.

Task 3: Choose a C++ Compiler

For instructions, see Choose a Compiler.

Task 4: Generate Code

For instructions, see Generate Code.

Integrate Custom C Code for Nonlibrary Charts for Code Generation

To integrate custom C code that applies to nonlibrary charts for embeddable code generation, perform the tasks that follow.

Task 1: Include Custom C Code for Embeddable Code Generation

Specify custom code options for Simulink Coder code generation of your model:

  1. Open the Model Configuration Parameters dialog box.

  2. In the Model Configuration Parameters dialog box, select Code Generation > Custom Code.

    The custom code options appear.

  3. Specify your custom code in the subpanes.

    Follow the guidelines in Specify Relative Paths for Custom Code.

      Note:   If you specified custom code settings for simulation, you can apply these settings to code generation. To avoid entering the same information twice, select Use the same custom code settings as Simulation Target.

Task 2: Generate Code

For instructions, see Generate Code.

Integrate Custom C Code for Library Charts for Code Generation

To integrate custom C code that applies only to library charts for embeddable code generation, perform the tasks that follow.

Task 1: Include Custom C Code in Embeddable Targets for Library Models

Specify custom code options in the embeddable target for each library model that contributes a chart to your main model:

  1. In the Stateflow Editor, select Code > C/C++ Code > Code Generation Options.

    The Model Configuration Parameters dialog box appears.

  2. In the Code Generation pane, select Use local custom code settings (do not inherit from main model).

    This step ensures that each library model retains its own custom code settings during code generation.

  3. Specify your custom code in the subpanes.

    Follow the guidelines in Specify Relative Paths for Custom Code.

      Note:   If you specified custom code settings for simulation, you can apply these settings to code generation. To avoid entering the same information twice, select Use the same custom code settings as Simulation Target.

  4. Click OK.

Task 2: Generate Code

For instructions, see Generate Code.

Integrate Custom C Code for All Charts for Code Generation

To integrate custom C code that applies to all charts for embeddable code generation, perform the tasks that follow.

Task 1: Include Custom C Code for Embeddable Code Generation of the Main Model

Specify custom code options for Simulink Coder code generation of your main model:

  1. Open the Model Configuration Parameters dialog box.

  2. In the Model Configuration Parameters dialog box, select Code Generation > Custom Code.

    The custom code options appear.

  3. Specify your custom code in the subpanes.

    Follow the guidelines in Specify Relative Paths for Custom Code.

      Note:   If you specified custom code settings for simulation, you can apply these settings to code generation. To avoid entering the same information twice, select Use the same custom code settings as Simulation Target.

Task 2: Ensure That Custom C Code for the Main Model Applies to Library Charts

Configure the embeddable target for each library model that contributes a chart to your main model:

  1. In the Stateflow Editor, select Code > C/C++ Code > Code Generation Options.

  2. In the Code Generation pane, clear the Use local custom code settings (do not inherit from main model) check box.

    This step ensures that library charts inherit the custom code settings of your main model.

  3. Click OK.

Task 3: Generate Code

For instructions, see Generate Code.

Was this topic helpful?