MATLAB Examples

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.

Contents

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 the following libraries that MathWorks® provides. If you are not sure which library to use, see docid:rtw_ug.buiqdva.

  • GNU C99 extensions—GNU® gcc math library, which provides C99 extensions as defined by compiler option -std=gnu99.
  • AUTOSAR 4.0—Produces code that more closely aligns with the AUTOSAR standard. Requires an Embedded Coder® license.
  • Intel IPP for x86-64 (Windows)—Generates calls to the Intel® Performance Primitives (IPP) library for the x86-64 Windows® platform.
  • Intel IPP/SSE for x86-64 (Windows)—Generates calls to the IPP and Streaming SIMD Extensions (SSE) libraries for the x86-64 Windows platform.
  • Intel IPP for x86-64 (Windows using MinGW compiler)—Generates calls to the IPP library for the x86-64 Windows platform and MinGW compiler.
  • Intel IPP/SSE for x86-64 (Windows using MinGW compiler)—Generates calls to the IPP and SSE libraries for the x86-64 Windows platform and MinGW compiler.
  • Intel IPP for x86/Pentium (Windows)—Generates calls to the IPP library for the x86/Pentium Windows platform.
  • Intel IPP/SSE for x86/Pentium (Windows)—Generates calls to the Intel Performance IPP and SSE libraries for the x86/Pentium Windows platform.
  • Intel IPP for x86-64 (Linux)—Generates calls to the IPP library for the x86-64 Linux® platform.
  • Intel IPP/SSE with GNU99 extensions for x86-64 (Linux)—Generates calls to the GNU libraries for IPP and SSE, with GNU C99 extensions, for the x86-64 Linux platform.

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 docid:rtw_ref.bq26cja-1 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 docid:rtw_ref.bq26b58-1.
  • 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 docid:rtw_ref.brh9ygm-4. Consider having the report open automatically. Select Open report automatically.

2. Include the code replacement section in the report. Select docid:rtw_ref.btaoq9q-1.

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