Code Generation Differences Across Platforms
R2026bWhen your code generation process uses multiple computers, differences between environments can lead to differences in the generated code. For example, if you develop models on a Windows® platform and integrate changes in a continuous integration (CI) pipeline on a Linux® platform, the code that you generate locally can be different from the code generated in the pipeline. You can control some of these factors to minimize differences by changing the model configuration for code generation. Some factors are inherent to the code generation environment. Consider these factors when you investigate differences in the generated code.
Target Settings That Depend on Development Environment
Some code generation configuration settings can detect the current development platform and automatically generate code for that environment. These options enable you to open a model and build code without manually changing the settings to match the current development computer. However, when using these settings, parts of the generated code that depend on them can differ across machines even if you do not change the model or its configuration settings.
Target Hardware Settings
The code generator produces code for the hardware specified by these model configuration parameters:
If you clear Test hardware is the same as production hardware and specify the test device as Custom Processor > MATLAB Host Computer, the code generator targets the current development computer. As a result, when you generate code on a different computer such as a CI pipeline machine, parts of the generated code that depend on the target hardware can differ across machines even when the parameter setting is the same.
For more information about production and test hardware considerations, see Configure Run-Time Environment Options.
Toolchain Settings
The code generator produces code for the toolchain specified by the Toolchain parameter. The default setting,
Automatically locate an installed toolchain,
selects a toolchain installed on the current computer based on the current MEX
compiler settings. This configuration means that even if you do not change this
parameter, the selected toolchain can be different when you generate code on a
different machine. As a result, parts of the generated code that depend on the
toolchain, such as compiler-specific optimizations, can differ across machines
even when the parameter setting has not changed.
To mitigate these potential differences, install the same toolchain on the computers that you use to generate code throughout your workflow. For the Toolchain parameter, specify that toolchain instead of automatically locating it.
Target Platform Differences That Affect Generated Code
To compile and execute, the generated code must conform to certain characteristics of the target platform. As a result, these aspects of the generated code might differ for different platforms.
Data Types
Depending on the data types available from the target platform, the code generator might need to use different data types to implement corresponding Simulink data types. To control how the code generator implements data types, see Manage Replacement of Simulink Data Types in Generated Code.
If data types are different because your test and production hardware have
different word sizes, you can configure the code generator to support data types
from both platforms by enabling Enable portable word sizes.
However, supporting different platform-specific data types generates larger
code, specifically, rtwtypes.h, because
rtwtypes.h still depends on the current platform. You can
avoid generating rtwtypes.h, and instead use data types from
the C99 language standard, by setting Data type
replacement to Use C data types with fixed-width
integers.
Literal Processing
Because different platforms represent and process literals in different ways,
literals can appear to be different in the generated code depending on the
platform, even if the underlying value is the same. For example, 64-bit integer
literals use the suffix LL or ULL on
Windows and L or UL on Mac and Linux.
To make generated literals more
consistent by using C99 macros for minimum-width integer constants, set
Data type
replacement to Use C data types with fixed-width
integers. (since R2026a)
Multithreading
If the generated code uses multithreading, associated constructs such as mutex and semaphores are platform dependent. You can replace the generated code for a semaphore or mutex with custom implementation code by using code replacement. For more information, see Semaphore and Mutex Function Replacement.
If you generate an example main program and set Target operating system to
NativeThreadsExample, the generated main program
uses a threading library supported by the current host computer.
Optimizations
Some optimizations are implemented differently in the generated code depending on the platform on which you generate code. For example, vectorized commands for single instruction, multiple data (SIMD) operations depend on the target hardware for which you generate code, so the default value of Leverage target hardware instruction set extensions changes depending on your target hardware.
You can make the generated code more portable and consistent across platforms by disabling platform-specific optimizations; however, the code will be less efficient.
Compiler Implementations of Standard Functions
The compiler defines the implementations for the standard functions that the
generated code calls, such as functions from math.h. If the
implementation of a function in the specified compiler might cause a numerical
mismatch between the generated code and simulation, the code generator replaces
the function with a custom implementation. The standard functions that get
replaced by custom functions can be different across platforms because of
differences in the compiler implementations of the standard functions.
For example, if your toolchain uses the MinGW® compiler, the code generator replaces the standard C function
roundf. To override replacements such as this, use a code
replacement library. For more information, see What Is Code Replacement?.
Model or Configuration Changes
If the generated code for a model changes, check for a change to the model or its configuration that you are not aware of. Simulink® uses the model's checksum to determine if changes to a model require regeneration of the code. You can use the checksum to investigate why generated code has changed.
These common scenarios that lead to model or configuration changes that might not be obvious:
The model references a configuration set that is stored externally, such as in a Simulink data dictionary.
The pipeline uses a script to set up the model in a way that depends on the current host platform.
The model includes setup commands, such as model callbacks or block callbacks, that configure the model differently depending on the host platform.
For an example of using the checksum to investigate changes to a model, see Determine Why Simulink Accelerator Is Regenerating Code.