Main Content

External Code Integration

Choose and apply integration paths and methods

The code generator includes a variety of approaches for integrating legacy or custom code with generated code. Legacy code is existing handwritten code or code for environments that you integrate with code that the code generator produces. Custom code is legacy code or other user-specified lines of code that you include in the code generator build process. Collectively, legacy and custom code are called external code.

You integrate external code by importing existing external code into code produced by the code generator, exporting generated code into an existing external code base, or you can do both. For example, you can import code by calling an external function, by using the Legacy Code Tool, or place external code at specific locations in generated code by including Custom Code blocks in a model. When you import external code, the resulting generated code interfaces with generated scheduling code.

You can export generated code as a plug-in function for use in an external development environment. When you export generated code, you intend to interface that code manually with a scheduling mechanism in your application run-time environment.

For guidance on choosing an approach based on your application requirements, see Choose an External Code Integration Workflow .


expand all

Model HeaderSpecify external header code
Model SourceSpecify external source code
System DerivativesSpecify external system derivative code
System DisableSpecify external system disable code
System EnableSpecify external system enable code
System InitializeSpecify external system initialization code
System OutputsSpecify external system outputs code
System StartSpecify external system startup code
System TerminateSpecify external system termination code
System UpdateSpecify external system update code


Getting Started

What Is External Code Integration?

What external code integration is.

Choose an External Code Integration Workflow

For application integration points, characterize external code, identify code generation integration requirements, and choose a workflow.

Untrusted Custom Code, Custom Targets, and Callbacks

Use of untrusted custom code entails risk.

Import External Code Into Generated Code

Call Reusable External Algorithm Code for Simulation and Code Generation

Call external algorithmic code, which is hardware-independent and reusable, from generated code.

Place External C/C++ Code in Generated Code

Place C/C++ code into code generated for a model.

Integrate External C++ Object Methods

Integrate legacy C++ object methods by using the Legacy Code Tool.

Call External Device Drivers

Call external device drivers from generated code.

Apply Function and Operator Code Replacements

Configure the code generator to replace functions and operators for alignment with external code or run-time environment requirements.

Build Integrated Code Within the Simulink Environment

Add support files and control model code generation and builds within the Simulink Environment.

Control Data and Code Interfaces

Exchange Data Between External C/C++ Code and Simulink Model or Generated Code

Configure the signals, states, and parameters in a Simulink® model to match the data interface of your existing C code.

Exchange Data Between External Calling Code and Generated Code

When you export the generated code into your external code, exchange signal, state, and parameter data.

How Generated Code Exchanges Data with an Environment

The generated code exchanges signal, state, and parameter data with the calling environment through a data interface.

Control Data and Function Interface in Generated Code

Control how generated code exchanges data with a calling environment.

Design Data Interface by Configuring Inport and Outport Blocks

Customize the data interface of a model to enable integration of the generated code with your own code and to improve code traceability and readability.

Control Generated Code to Match Other Integration Requirements

Generate Code That Matches Appearance of External Code

Configure how the code generator applies identifiers (symbols), comments, code styles, code format, and organization.