Sharing is Caring…So call your DLL to say “I love you!”
Greg's pick this week is Calling Shared Libraries from Simulink by Mikhail.
Perhaps you have a DLL that you want to access from a Simulink model. Maybe a colleague has developed an algorithm in another language that can be compiled as a shared library, and you want to take advantage of it.
How can you access it from Simulink?
If you're familiar with Simulink, you're probably groaning as the words "S-Function" cross your mind, but there are other options, which can be easier.
Mikhail's entry that describes a variety of methods you can apply to access the functions and variables that are exposed in the shared library from Simulink.
What is a shared library and why would I use it?
This explanation of shared libraries covers the basics. Shared libraries make for a relatively portable means of sharing functionality between executables. I say relatively because shared libraries are specific to an operating system. On Windows they are DLL-files and Linux they are SO-files.
Your colleague has written some set of functions in C-code used for some other application. If your colleague compiles these functions to be exposed via a DLL, each of you can leverage the functions from your own applications, without needing to share the source code. (Although you generally need to know the interface definitions of the exposed functions)
What are the methods available to call a shared library?
Mikhail has a very nice document that describes several methods for accessing a shared library from Simulink.
- C-code S-function using Legacy Code Tool
- MATLAB Function block using coder.ceval
- MATLAB System block using coder.ceval
As part of the examples, it also demonstrates how to load a shared library into MATLAB using the LOADLIBRARY function.
And (my favorite) demonstrates how to load a library that was generated from Simulink using Embedded Coder.
Do people actually do this?
Yes, search "loadlibrary" on the MATLAB File Exchange to give you an idea of how people might use this type of capability.
Why did I choose this entry?
To start, this entry is well formed and well documented.
Also for me, coming across this entry is very timely. I have been working on some projects that involve deploying Simulink models as MEX-files. Notice that you can generate a DLL from Simulink, and then load that DLL into MATLAB. I have used that very method, but instead of using LOADLIBRARY, I call the DLL from a MEX-file that has been compiled for MATLAB.
How important is this capability in Simulink?
- Do you need to access shared libraries from your Simulink model?
- Do you want access to simulations exported from other tools?
- Should C-code generation support calling a function in a DLL?
- Is it useful to call Simulink models as a MEX-function?
Let us know here.
To leave a comment, please click here to sign in to your MathWorks Account or create a new one.