FMU Code Generation Support from Simulink & Platform Compatibility Considerations

29 views (last 30 days)
My Simulink model includes an imported FMU in co-simulation mode. I am planning to generate code from my model for integration on external hardware for a HIL simulation. How can I ensure the FMU is suitable for this workflow?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 6 Oct 2025 at 0:00
Edited: MathWorks Support Team on 6 Oct 2025 at 12:50
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:
  1. If the FMU includes source code (sources/ folder), you can compile it for your target environment using the appropriate compiler and build settings.
  2. 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.
If the FMU was exported from Simulink, there is an option to save the source code to the FMU (requires Simulink Coder for GRT targets and Embedded Coder for ERT targets). See the following MATLAB Answer for more information on cross-compiling FMUs.

⚙️ 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.
2) For Speedgoat targets and Simulink Real-Time, there is an integrated "slrealtime.fmu.compileFMUSources" cross-compilation function that you can use. See also this MATLAB answer.
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:

More Answers (0)

Categories

Find more on Deployment, Integration, and Supported Hardware in Help Center and File Exchange

Products


Release

R2024b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!