Main Content

Replace Code Generated from Simulink Models

This example shows how to replace generated code, using a code replacement library. Code replacement is a technique you can use to change the code that the code generator produces for functions and operators to meet application code requirements. For example, you can replace generated code to meet requirements such as:

  • Optimization for a specific run-time environment, including, but not limited to, specific target hardware.

  • Integration with existing application code.

  • Compliance with a standard, such as AUTOSAR.

  • Modification of code behavior, such as enabling or disabling nonfinite or inline support.

  • Application- or project-specific code requirements, such as elimination of math.h, system header files, or calls to memcpy or memset, or use of BLAS.

Prepare for Code Replacement

1. Make sure that MATLAB®, Simulink®, Simulink Coder™, and a C compiler are installed on your system. Some code replacement libraries available in your development environment can require Embedded Coder®.

To install MathWorks® products, see the MATLAB installation documentation. If you have installed MATLAB and want to see which other MathWorks products are installed, in the Command Window, enter ver.

2. Identify an existing Simulink model or create a model for which you want the code generator to replace code.

Choose a Code Replacement Library

By default, the code generator does not apply a code replacement library. You can choose from libraries that MathWorks® provides and that you create and register by using the Embedded Coder® product. The list of available libraries depends on:

  • Installed support packages.

  • System target file, language, standard math library, and device vendor configuration.

  • Whether you have created and registered libraries, using the Embedded Coder® product.

Libraries that include GNU99 extensions are intended for use with the GCC compiler. If you use one of those libraries with another compiler, generated code might not compile.

Depending on the product licenses that you have, other libraries might be available. If you have an Embedded Coder license, you can view and choose from other libraries and you can create custom code replacement libraries.

Configure Code Generator to Use Code Replacement Library

1. Configure the code generator to apply a code replacement library during code generation for the model. Do one of the following:

  • In the Configuration Parameters dialog box, on the Code Generation > Interface pane, select a library for the Code replacement library parameter.

  • Set the CodeReplacementLibrary parameter at the command line or programmatically.

2. Configure the code generator to produce code only (not build an executable program) so you can verify your code replacements before building an executable program. Do one of the following:

  • In the Configuration Parameters dialog box, on the Code Generation pane, select Generate code only.

  • Set the GenCodeOnly parameter at the command line or programmatically.

Include Code Replacement Information in Code Generation Report

If you have an Embedded Coder license, you can configure the code generator to include a code replacement section in the code generation report. The additional information can help you verify code replacements.

1. Configure the code generator to generate a report. In the Configuration Parameters dialog box, on the Code Generation > Report pane, select Create code generation report. Consider having the report open automatically. Select Open report automatically.

2. Include the code replacement section in the report. Select Summarize which blocks triggered code replacements.

Generate Replacement Code

Generate C/C++ code from the model and, if you configured the code generator accordingly, a code generation report. For example, in the model window, press Ctrl+B.

The code generator produces the code and displays the report.

Verify Code Replacements

Verify code replacements by examining the generated code. It is possible that code replacement behaves differently than you expect. For example, data types that you observe in the code generator input might not match what the code generator uses as intermediate data types during an operation.

More About