Integrate FMI APIs in Generated Code Using FMU Source Code or Binary
This example demonstrates the two methods for generating code for a Functional Mockup Unit (FMU) block in Simulink®. You can generate code for the FMU using the FMU source code or its compiled binaries. The code generated using FMU source code is independent of the FMU binary, allowing it to be compiled across multiple platforms. The code generated using FMU binary is dependent on the platform-specific binaries.
Code generation enables you to perform these function for a model with FMU:
Rapid Accelerator simulation mode
Generation of GRT and ERT target code
Nested FMU generation
Source code based code generation is supported for co-simulation and model exchange FMUs compatible with FMI versions 2.0 and 3.0. You can generated code using compiled binaries for co-simulation and model exchange FMUs that are compatible with FMI version 1.0, 2.0, and 3.0.
Generate Code for FMU Import Block
According to the FMI standards, FMUs are packaged with platform-specific compiled binary files and can also optionally contain the FMU source files. These binaries and source files contain the FMI functions needed by the FMU Import block for simulation of the FMU.
In source code based code generation of the FMU Import block, FMI APIs from the FMU source code is inlined in the generated code. In binary-based code generation of the FMU Import block, the generated code loads the FMI APIs from the shared library and references these using function pointers.
To specify the type of code generation for your FMU, in the Code Generation tab of the FMU block dialog, specify the Call FMU functions using option. Set this option as source code for source code based code generation and as shared library for binary based code generation.
Open the vdp_FMUModel
model. This model includes an FMU Import block that loads vdp.fmu
, which contains both binary and source code. You can use the fmudialog.compileFMUSources
to generate the binary for your platform using the source code of the FMU.
modelVdp = 'vdp_FMUModel'; load_system(modelVdp); if ~ispc % FMU only contains windows binary. Generate binary for Linux and Mac fmudialog.compileFMUSources("vdp.fmu"); end
Compiling artifacts from source code: 'vdp.c,vdp_data.c,RTWCG_FMU_util.c,vdp_fmu.c'. Shared library is generated at location: '/tmp/Bdoc25a_2869801_325841/tpfb225f27/simulink_features-ex27753611/vdp/binaries/linux64/vdp.so'. FMU is generated at location: '/tmp/Bdoc25a_2869801_325841/tpfb225f27/simulink_features-ex27753611/vdp.fmu'.
To generate GRT code for this FMU, in the Simulink toolstrip, navigate to the APPS tab and select Simulink Coder. Configure the code generation settings and build the model to generate code. For more information on generating code, see Generate Code Using Simulink Coder (Simulink Coder). By default, the FMU Import block enables source code based code generation for FMUs generated by Simulink from R2025a. The FMU binary is used for code generation if the FMU does not contain its source code.
Set the Call FMU functions using option as source code and build the model.
fmublock = [modelVdp '/vdpFMU']; set_param(fmublock, 'FMUCGType', 'source code');
Open the code generation report to verify that FMI APIs are inlined in the generated code and the code is independent of the compiled binary.
To generate code using the FMU binary, set the Call FMU functions using option as shared library and build the model.
set_param(fmublock, 'FMUCGType', 'shared library');
Open the code generation report to verify that in the generated code loads the compiled binary and references the FMI APIs using function pointers.
Copyright 2025 The MathWorks, Inc.