Since R2023b: Binary-based code generation for FMUs
Starting with MATLAB R2023b, Simulink supports code generation from FMUs using FMI 1.0 and FMI 2.0. R2024a adds code generation support for FMI 3.0. This method loads the precompiled binary (.dll/.so) and uses function pointers to access FMI APIs.
🖥️ Platform compatibility:
To ensure portability, the FMU must include the precompiled binary (dynamic shared library) compatible with your target platform. For example, if your FMU only includes a Windows binary (binaries/win64/<model>.dll) but you're targeting a Linux-based platform, you have two options:
- If the FMU includes source code (sources/ folder), you can compile it for your target environment using the appropriate compiler and build settings.
- If you don't have source code, then you must ask the supplier of the FMU to provide a compiled target binary for the target environment.
⚙️ Considerations for targeting hardware:
1) The binary-based approach for FMUs is mainly for GRT targets and desktop simulations. ERT targets and embedded hardware support is limited, and would require hardware that can load shared libraries. Hardware like microcontrollers typically do not support dynamically loading shared libraries at runtime because there’s no general-purpose operating system or file management layer.
Since R2025a: Source-code-based code generation for FMUs
Starting in R2025a, you can directly enable cross-compilation for the generated code by using the FMU's packaged source code. Source-code-based code generation is the default setting for nested FMUs, ERT targets, GRT targets, and rapid accelerator simulation mode. This method compiles a static shared library directly from the FMU source code. With that, FMI APIs are inlined, making the generated code independent of any compiled binary.
Compared to the binary-based approach, the new source-code-based approach:
- ✅ Requires no precompiled binary.
- ✅ Is suitable for embedded hardware that doesn't support dynamic library loading.
- ⚠️ Works only if FMU sources are available.
- ⚠️ Cannot be guaranteed if the FMU wasn't exported from Simulink.
- ⚠️ Officially supported only for GRT target. May still have some limitations for ERT targets.
To generate code based on the source code of your FMU, in the Code Generation tab of the FMU block dialog, set the Call FMU functions using option as source code:For an example and details regarding the generated code and used FMI functions, see: