Main Content

Compile Source Code for Functional Mock-up Units

When you build a model that includes FMU blocks, you must compile the FMU source code by using the QNX® Neutrino® compiler qcc or q++. This compiler creates shared object SO files that you include in the FMU. This process makes sure that the FMU contains the code to run on a Simulink® Real-Time™ target computer. For more information, see Apply Functional Mock-up Units by Using Simulink Real-Time.

Configure Compiler Environment Variables

The support package for the target computer includes the QNX Neutrino C/C++ compiler qcc or q++. Before using the compiler to generate the FMU file, configure the compiler environment variables.

Open a Windows cmd window and change folders to the root folder for the Simulink Real-Time Target Support Package. If you have changed the default folder for MATLAB support packages, adjust this command to match your custom location. At the Windows command prompt, type:

cd C:\ProgramData\MATLAB\SupportPackages\<release>\toolbox\slrealtime\target\supportpackage\qnx710

To set the Windows environment variables that are required to use the QNX Neutrino compiler from the command line, run the batch file qnxsdp-env.bat. At the Windows command prompt, type:


Ensure that the qcc compiler is ready to use. At the Windows prompt, type:

which qcc

The command returns:


Create the FMU File

The FMU file contains a hierarchy of files and folders. A set of these is provided in this example. The example shows how to create the shared object file for a single source file that is linked in the real-time application that runs on the target computer.

To view the files for this example, in the MATLAB Command Window, type:

cd(fullfile(matlabroot, 'toolbox', 'slrealtime', ...
'examples', 'slrt_ex_fmu_work'))

To open the model for this example, in the MATLAB Command Window, type:

open_system(fullfile(matlabroot, 'toolbox', 'slrealtime', ...
'examples', 'slrt_ex_vanderpol'))

The example FMU file vanDerPol_slrt.fmu contains this set of folders:


The example has FMU source files in the sources folder. This example uses files: fmuTemplate.c, fmuTemplate.h, vanDerPol.c

The example has a dynamically linked library file from the source files that was created by using a Window-based compiler. This file is in the win64 folder. This example uses file: vanDerPol.dll

To compile the sources in the example by using qcc, copy the files for the FMU from folder matlab/slrealtime/examples/slrt_ex_fmu_work to folder C:\work\my_fmu_work.

In a Windows cmd window, change the current folder to be the location of your source files. At the Windows command prompt, type:

cd C:\work\my_fmu_work\sources

To generate the QNX Neutrino shared object SO file, the example compiles the sources by using the -Vgcc_ntox86_64 flag. At the Windows command prompt, type:

qcc -Vgcc_ntox86_64 -shared -o ../binaries/slrt_x64/ -fPIC -DFMI_COSIMULATION -IC:/PROGRA~1/MATLAB/R2021b_bash/rtw/c/src/rapid/fmu/fmi1 vanDerPol.c


The QNX Neutrino compiler does not support Windows long file names (for example, paths with space characters) for the library search switch -I. To work around this limitation, you can install MATLAB® in a path without spaces (for example, C:\MATLAB) or you can use the DOS 8.3 path in the compiler command. To get the 8.3 folder names, you can use the dir /x command in a Windows cmd window.

To generate the functional mockup unit FMU file that contains the QNX Neutrino shared object file, the example archives the folders and files. Use the FMU extension for the archive file. At the Windows command prompt, type:

cd ..
zip -r vanDerPol.fmu *

Implement the FMU Block in Model

To implement the vanDerPol block in the Simulink model by using the FMU, specify the FMU name for the block. Open the model slrt_ex_vanderpol, double-click the FMU block vanDerPol, and select the vanDerPol.fmu file for the FMU name block parameter.

Build the model, load the real-time application on the target computer, and run the real-time application.

Compile FMU File That Contains Source Code

The slrealtime.fmu.compileFMUSources function compiles an FMU file that contains source code. The process outputs an FMU file and Simulink Real-Time binary file in the same folder as the input FMU file and appends an _slrt suffix to the output file name. This example selects an FMU file to compile and overwrites previous compiler output.

% create variable to provide path and file name
my_file = ['C:\work\my_fmu_work\','vanDerPol.fmu']
% compile the FMU file and overwrite previous output

See Also


Related Topics

External Websites