How to port S-function across different architectures?

5 views (last 30 days)
I am dealing with the following problem. I am developing a custom Simulink block, based on C/C++ code in an S-Function. The code itself does not depend on libraries, or better, it depends on system libraries that are included with #ifdefs brackets in order to have a cross-platform implementation of the S-function.
However, I'm figuring out how to make the S-function portable, since the files that Simulink calls are pre-compiled depending on which host run the "Build" command in the S-function builder.
I am willing to make the source code available for the user to compile, but I still cannot figure which is the best way to make the user experience smooth. With libraries, I could just save the Simulink library and send the .slx file. With S-function, I could send the .c code but then the user has to load it into the S-function builder, run build, etc... it's a bit cumbersome.
I found out that if I export a library with an S-function builder block, the code inside the block is retained when I send the library to other users. Morover, running the system automatically builds the S-function according to the architecture that is running the simulation. Is this the proper way to handle this issue? Should I send out Simulink libraries with S-function builder blocks with the portable C/C++ code inside? Or there's a better alternative?
I also don't particularly like the fact that the S-function build files are located in the current directory; is there any way to make the S-function builder output the files in a custom directory and automatically load the compile .mexXXX file from there without the user needing to tweak with the settings?

Answers (1)

Sayan on 4 Dec 2023
Hi Gianfranco Di Domenico,
I understand from your query that you need an alternative to S-Function Builder to make the Simulink block created from it more portable across different architectures. As you agree on sharing the source code, the "Legacy Code Tool" can be an alternative to this. If you have existing C/C++ source code, the "Legacy Code Tool" can be used for integration.
You can find the following steps to achieve the same:
  • Open a new MATLAB "Script" to specify the "Legacy Code Tool" data structure and define the name for the S-function, specifications for the existing C/C++ functions,files and paths required for compilation, and options for the generated S-function.
  • Use the "legacy_code" function to initialize the Legacy Code Tool data structure for a given C/C++ function,generate an S-function for use during simulation,compile and link the generated S-function into a dynamically loadable executable, and generate a masked S-function block for calling the generated S-function.
You can share the source files along with the MATLAB "Script" file. The user can run the "Script" which will automatically load the ".mexXXX" file in a S-function block and create the custom Simulink block in the model.
You can find more information on "Integrate C Functions Using Legacy Code Tool", "legacy_code" and "S-Function" block in the following documentations.
Hope this helps in answering the query.




Community Treasure Hunt

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

Start Hunting!