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


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 (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (19)

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.

Any chance to have the submission also with 2017b?



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


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.


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.

Ander Biguri

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


how can i open these attachments ??


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



Validated with R2019a


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

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

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

Validated with MATLAB R2017a

Validated with MATLAB R2016b

Verify with 16a

Updated to R2015a, cosmetic changes.

This Toolbox checkbox keeps poping up

Add information about Embedded Coder

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

Add examples for MATLAB Function, MATLAB System, Stateflow

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