Editor's Note: This file was selected as MATLAB Central Pick of the Week
This paper describes various approaches for calling shared libraries from Simulink models. Both explicit (load-time) and implicit (run-time) linking is considered.
Mikhail (2021). Calling Shared Libraries from Simulink (https://www.mathworks.com/matlabcentral/fileexchange/44197-calling-shared-libraries-from-simulink), MATLAB Central File Exchange. Retrieved .
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Create scripts with code, output, and formatted text in a single executable document.
I think it is safe to say that all the described methods support passing pointers in and out. Choose your preferred method and go ahead into documentation for more details. For example, here is one for LCT: https://www.mathworks.com/help/simulink/slref/inputs-passed-by-value-or-address-to-legacy-functions.html
do you have any idea for using pointer-based .lib in S-Function? I want to use pointer in simulink
Thanks, it works now
@Stephane You should use explicit linking (as opposed to implicit linking with import *.lib file). Manually written S-function (possibly combined with Legacy Code Tool to generate initial S-function) is the best approach. The article explains it all.
Your workflow invloves the lib file and not the dll file: how can I call a DLL file in Simulink (I have an header file but not the source code and not the lib file) ?
I've tried to use the legacy_code but if I replace the field HostLibFiles from exlib.lib to exlib.dll, when I run legacy_code('generate_for_sim',specs);, MATLAB says mex don't know dll
Easy to understand. Works really well.
@Alexandru, you can change the simulink preferences under (Preferences -> simulink -> Model file -> uncheck the "Do not load models created with a newer version of Simulink"), you will be able to open this in 2017b.
@Alexandru I'm keeping the models up to date with the current release of MATLAB. If you are using an older release, it is easy to reproduce the models - they are trivial (3 blocks) and screenshots are provided.
Hi,
Any chance to have the submission also with 2017b?
Thanks
@Jonathan you were probably using MinGW compiler. This should be addressed in the latest version of this submission.
The legacy_code method will also work for void-void functions without arguments.
I'm having problems replicating the "Manual 2-Level" method. Getting errors that the dllHandle, LoadLibrary are FreeLibrary functions are undeclared/unresolved when mex tries to link. Where are these defined in the sample code?.
Also, the legacy_code method I assume will only work properly if the core function uses arguments (i.e. to define at least one input and one output port) when the mexw32 is used in an s-FUNC?
Perfect!
I was wondering whether it is possible to call .dll created from simulink blocks with a Matlab executable. It seemed that one cannot do that since the sim command cannot be compiled into the MatLab executable. All the examples in this tutorial seemed to all require Simulink license to run.
To make S-functions work with Rapid Accelerator mode, you need to make them inline.
This is way beyond this article - either read the documentation or contact MathWorks Tech Support/Consulting for these issues.
Everything works so nice till we test in Accelerator mode. That means all mex works with the examples. But while we build or run in rapid accelerator mode all of them report Linker error. There seems to be some path issues or makefile problem. Your help is greatly appreciated if you can provide a fix for the same. Lot of people reported Linker error for Simulink builds with U1077, U1073.
Thanks in advance.
Great Tutorial, It's really helped me, Thanks.
how can i open these attachments ??
simlib_test_dyn.slx
simlib_test_mlf.slx
simlib_test.slx
simlib_test_sf.slx
simlib_test_ert.slx
simlib_test_callert.slx
simlib_test_mls.slx
A good tutorial. Nice coverage of six (or seven) different ways to use c shared libraries in Simulink/Matlab.