File Exchange

image thumbnail

Calling Shared Libraries from Simulink

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

34 Downloads

Updated 31 May 2019

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 (2019). 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 (20)

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.

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.

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.

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.

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!

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

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.

Updates

4.0

Validated with R2019a

3.1

Fixed "MATLAB Function" model to work out of the box.

3.0.0.0

- Validated with R2018b
- Added support for MinGW
- Added description of C Caller block

2.0.0.0

Update all models to R2017a;
Change wording and add clarifications;
Support code generation.

1.9.0.0

Validated with MATLAB R2017a

1.8.0.0

Validated with MATLAB R2016b

1.7.0.0

Verify with 16a

1.6.0.0

Updated to R2015a, cosmetic changes.

1.5.0.0

This Toolbox checkbox keeps poping up

1.4.0.0

Add information about Embedded Coder

1.3.0.0

It's not a Toolbox. Very smart to have this checkbox ticked by default..

1.2.0.0

Add examples for MATLAB Function, MATLAB System, Stateflow

1.1.0.0

Add more clarifications and links, change wording, more items in TODO.

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