File Exchange

image thumbnail

From Simulink to DLL - A tutorial

version 1.0 (67.8 KB) by

Describes how to build a DLL or shared library from a Simulink model.



No License

This document attempts to give a hands-on description on how to make a DLL from a controller built in Simulink. This makes it possible to develop advanced controllers in Simulink, and then use them from an arbitrary application. Persons with knowledge in the C programming language should have no problems converting the steps in this tutorial to build Linux/Unix libraries, thus expanding the scope of this document to be applicable not only on Windows.

Comments and Ratings (20)


Good for me.


G (view profile)

need some help


I have an applicatoin:

generate DLL from a simulink model, DLL is called in an external c program, when DLL initializes, it will load mat file and variables in m files.

can i change the m files without recompile the DLL?

Need some help on this.

When I have generated the code and update the grt_shell.c file for inputs and outputs I tried to build the DLL as mentioned in the section 4.6. when I execute the command gmake –f ($NAMEOFMODEL).mk I am getting an error “:128: *** missing separator. Stop.”.
I understand that this is caused due to the use of space in place of tabs. But as you might know the .mk file is generated by the RTW. I checked the line 128 in the .mk file and it has no space at all.

Any suggestions would be much appreciated.

We are getting the error ' NMAKE : fatal error U1073: don't know how to make 'cn_main.ofile'
The make command returned an error of 2
'An_error_occurred_during_the_call_to_make' is not recognized as an internal or external command,
operable program or batch file.

This is appeared when we try to build the CANoe supported model. We are trying to interface Matlab into CANoe using the dll generated by the Matlab. But dll is not generated. Can anybody have a answer, please let us know.


Imran (view profile)

i can follow till 5th step of 4.6
than gmake gives me this out put
C:\123\add1_grt_rtw>gmake -f
C:/PROGRA~1/MATLAB/R2008b\sys\lcc\bin\lcclnk -s -LC:/PROGRA~1/MATLAB/R2008b\sys\
lcc\lib -o ../add1.exe add1.obj add1_data.obj rtGetInf.obj rtGetNaN.obj rt_log
ging.obj rt_nonfinite.obj grt_main.obj rt_sim.obj @add1_ref.rsp
*** Created executable: add1.exe

this donot match with step 4.6 - 6 so what am i doing wrong?
please help i need it real bad and real fast
i am using make 3.81 compiler

hii.. i cannot create dll. the error shows that
""you must define MSDevDir"
does anyone know about this?


Gianni (view profile)


Gianni (view profile)

hi everybody.
I have a problem. when I attemp to use gmake -f ($NAMEMODEL).mk I receive this error:
??? Undefined function or method 'gmake' for input arguments of type 'char'.
if I use the command :
!gmake -f ($NAMEMODEL).mk
it works but I can not see what gmake is dooing, the only output is :
>> !gmake -f
gmake: `../controller.exe' is up to date.

can someone explain me hot to solve this problem? or please send me the code that I need to continue?
Many thanks,

Nick Clark

Nick Clark (view profile)

Good stuff here. Thanks! I'd just like to add a little bit. For those who don't know pascal, please see the python script pasted below. This can be used to test your dll. This specific example is for 3xinput and 2xoutput:

from ctypes import *

# first make some hooks into the impported functions
initiateController = cdll.controller._initiateController
getControllerOutput = cdll.controller._getControllerOutput
performCleanup = cdll.controller._performCleanup

# This makes python realise that the function outputs are pointers to c_chars
initiateController.restype = c_char_p
getControllerOutput.restype = c_char_p
performCleanup.restype = c_char_p

print "\n\nInitiate"
errMsg = initiateController()
print "initiateController reports : ", errMsg

print "\n\nNow to do some stuff"
inputDoubles = 3*c_double
inputData = inputDoubles(2.5, 3.0, 1.0)
p_inputData = pointer(inputData)

outputDoubles = 2*c_double
outputData = outputDoubles(0.0, 0.0)
p_outputData = pointer(outputData)

#The following line shows two different methods for passing the pointers in
errMsg = getControllerOutput(c_int(3), p_inputData, c_int(2), byref(outputData) )
print "getControllerOutput reports : ", errMsg
print "output[0] = ", outputData[0], "\noutput[1] = ", outputData[1]

print "\n\nCleanup"
errMsg = performCleanup()
print "performCleanup reports : ", errMsg

Nice information, thank you


I had a similar error to Dennis from a project I was compiling and linking into a project here at work. Turns out I hadn't set up the Configuration Parameters correctly (Tools -> Real Time Workshop -> Options). After I had set these up appropriately the error went away.

Dennis Jimmy

Hello Roland Pfeiffer,
     Thanks the document "from simulink model to dll",It is very helpful to me.
     but there is some error.
when I construct a project in vc2005, the simulink model dll is call in a simple main function.
when I build it , the error is happen; the error is
simstruc.h(282) : fatal error C1189: #error : Unrecognized use.

liu hz

Rajan R

Dennis Wouters,
I am new to matlab and RTWEC, I am not able to find the custom target example. can you please guide / share the information.

Dennis Wouters

This document really kick-started my work. Thanks.
I ended up developing a more general approach that requires only a little more programming.

* Derive a custom TLC from the grt.tlc file using the "Custom Target" example in the RTW Embedded Coder documentation, which does NOT require RTWEC, only RTW. (This is easy.)
* Derive a custom TMF from the grt_lcc.tmf file. Make the changes proposed in the RTWEC example as well as the changes that Roland proposes to make to the 'lcc' and 'lcclnk' command line options. (Not too hard, you only need to change 2 or 3 lines.)
* Finally derive a C file as described by Roland. (This is the most work, but is outlined very well in the article.)

This will give you the opportunity to configure the model to build directly to a DLL without any manual intervention!

Happy DLL'ing!

Diego Barragán


Jaspreet kaur

I want to use matlab program in VB

Scott Reeder

Very good tutorial. I would recommend adding RTW to the keywords for this tutorial.

Stefan Lindhuber

Excellent step-by-step tutorial. It helped me to save a lot of time.

MATLAB Release
MATLAB 7 (R14)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video