File Exchange

image thumbnail

Calling Shared Libraries from Simulink

version 5.0 (280 KB) by Mikhail
This paper discusses several approaches for calling shared libraries from Simulink models

30 Downloads

Updated 02 May 2020

View Version History

View License

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.

Cite As

Mikhail (2021). Calling Shared Libraries from Simulink (https://www.mathworks.com/matlabcentral/fileexchange/44197-calling-shared-libraries-from-simulink), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (23)

Mikhail

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

bilal erol

do you have any idea for using pointer-based .lib in S-Function? I want to use pointer in simulink

Pavel Kurguz

Furkan Bayrak

Stephane Dauvillier

Thanks, it works now

Mikhail

@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.

Stephane Dauvillier

Stephane Dauvillier

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

Niravkumar Ambaliya

Easy to understand. Works really well.

Itamar Engelman

@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.

Mikhail

@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.

Codrut Alexandru Dan

Hi,
Any chance to have the submission also with 2017b?

Thanks

Mikhail

@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.

Jonathan Stevenson

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?

Karim Rostom

Perfect!

Martin Butcher

Ted Yang

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.

Mikhail

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.

Akash

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.

Ander Biguri

Or Hirshfeld

Great Tutorial, It's really helped me, Thanks.

arwa

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

Matthew

A good tutorial. Nice coverage of six (or seven) different ways to use c shared libraries in Simulink/Matlab.

MATLAB Release Compatibility
Created with R2020a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories

Community Treasure Hunt

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

Start Hunting!