Giving additional resource(.so file) to an s-function and generate FMU

I am generating FMU from a matlab s-function. The s-function needs an additional resource (.so file) to run and this is made available in the current path. No reference or definition given in s-function for this additional resource( I don't know how to give)
Issue: The FMU runs only when the additional resource (.so file) is in the current path else Matlab crashes. I want to run the FMU by having this additional resouce (.so file) inside FMU so that it can run on another PC. I tried creating a "resources/filename.so" inside MyModel.fmu unfortunately it doesn't helps.
Any thoughts?

댓글 수: 2

Did you try 'addpath' to add the path of the .so file?
Thanks @Meg Noah for your suggestion.
Yes. It works for running the sfunction. After that I am generating FMU and it fails to find the file while running the FMU. Also, manually copying the file inside FMU doesn't help

댓글을 달려면 로그인하십시오.

답변 (1개)

Arushi
Arushi 2025년 8월 8일
Hi Mayur,
If your S-function depends on a .so file and you're trying to generate a portable FMU that works across different machines, here's what worked best for me:
  • Include the .so file in the resources/ folder when exporting the FMU.
  • Modify your S-function C/C++ code to dynamically load the .so using dlopen() with the full path.
void* handle = dlopen("myLibrary.so", RTLD_NOW);
  • At runtime, the FMU is automatically extracted to a temporary directory. You can build the path to the .so file like this:
char path[PATH_MAX];
sprintf(path, "%s/resources/myLibrary.so", modelResourcePath);
void* handle = dlopen(path, RTLD_NOW);
modelResourcePath can be obtained from the FMU runtime or passed in using the FMI callback functions, depending on your setup.
This approach makes the FMU completely self-contained and portable.
Hope this helps!

댓글 수: 3

Mayur
Mayur 2025년 8월 11일
편집: Mayur 2025년 8월 11일
Hi,
Thank you @Arushi for your valuable input. I have modified the sfunction. But unfortunately I have few questions:
During the first test, I want to run the FMU in a simulink environment only. For this comment "modelResourcePath can be obtained from the FMU runtime or passed in using the FMI callback functions". How to define a modelResourcePath in sfunction?
"Include the .so file in the resources/ folder when exporting the FMU"
I am wondering how this step can be achieved before creating FMU? Since I am using Matlab 2021a with FMI 2.0. It doesn't have the the ability to add resources. After the FMU is created I unzip it and manually add to to anew resource folder.
Hi Mayur,
Since you’re on MATLAB R2021b, you can use the built-in Additional Files option when exporting the FMU to include your .so dependency directly inside the FMU package. This removes the need to unzip/re-zip the FMU manually.
In the “Additional Files” section of the export dialog, you can specify:
  • binaries/<arch>/ for dependent libraries loaded automatically by the FMU binary, or
  • resources/ for files you plan to load manually (e.g. dlopen in your S-Function).
Once you add your .so here and adjust your S-Function to build the absolute path at runtime, your FMU should run without needing the .so in the working directory.
Hi,
The 2021b version has a limitation to add additonal resources.
Also, check this dialog box in the attached image.

댓글을 달려면 로그인하십시오.

태그

질문:

2025년 8월 5일

댓글:

2025년 8월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by