{"id":6891,"date":"2016-05-06T09:00:47","date_gmt":"2016-05-06T13:00:47","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=6891"},"modified":"2017-08-11T13:24:15","modified_gmt":"2017-08-11T17:24:15","slug":"sharing-is-caring-so-call-your-dll-to-say-i-love-you","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2016\/05\/06\/sharing-is-caring-so-call-your-dll-to-say-i-love-you\/","title":{"rendered":"Sharing is Caring&#8230;So call your DLL to say &#8220;I love you!&#8221;"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/32620\">Greg<\/a>'s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/44197-calling-shared-libraries-from-simulink\">Calling Shared Libraries from Simulink<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/2945398-mikhail\">Mikhail<\/a>.\r\n      <\/p>\r\n      <p>Perhaps you have a DLL that you want to access from a Simulink model. Maybe a colleague has developed an algorithm in another\r\n         language that can be compiled as a shared library, and you want to take advantage of it.\r\n      <\/p>\r\n      <p>How can you access it from Simulink?<\/p>\r\n      <p>If you're familiar with Simulink, you're probably groaning as the words \"S-Function\" cross your mind, but there are other\r\n         options, which can be easier.\r\n      <\/p>\r\n      <p>Mikhail's entry that describes a variety of methods you can apply to access the functions and variables that are exposed in\r\n         the shared library from Simulink.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">What is a shared library and why would I use it?<\/a><\/li>\r\n         <li><a href=\"#2\">What are the methods available to call a shared library?<\/a><\/li>\r\n         <li><a href=\"#3\">Do people actually do this?<\/a><\/li>\r\n         <li><a href=\"#4\">Why did I choose this entry?<\/a><\/li>\r\n         <li><a href=\"#5\">How important is this capability in Simulink?<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>What is a shared library and why would I use it?<a name=\"1\"><\/a><\/h3>\r\n   <p>This <a href=\"http:\/\/en.wikipedia.org\/wiki\/Library_(computing)#Shared_libraries\">explanation<\/a> of shared libraries covers the basics.  Shared libraries make for a relatively portable means of sharing functionality between\r\n      executables.  I say relatively because shared libraries are specific to an operating system. On Windows they are DLL-files\r\n      and Linux they are SO-files.\r\n   <\/p>\r\n   <p>Your colleague has written some set of functions in C-code used for some other application. If your colleague compiles these\r\n      functions to be exposed via a DLL, each of you can leverage the functions from your own applications, without needing to share\r\n      the source code. (Although you generally need to know the interface definitions of the exposed functions)\r\n   <\/p>\r\n   <h3>What are the methods available to call a shared library?<a name=\"2\"><\/a><\/h3>\r\n   <p>Mikhail has a very nice document that describes several methods for accessing a shared library from Simulink.<\/p>\r\n   <div>\r\n      <ul>\r\n         <li>C-code S-function using <a href=\"https:\/\/www.mathworks.com\/help\/rtw\/ug\/legacy-code-tool-code-insertion.html\">Legacy Code Tool<\/a><\/li>\r\n         <li><a href=\"https:\/\/www.mathworks.com\/help\/simulink\/ug\/what-is-a-matlab-function-block.html\">MATLAB Function<\/a> block using <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/coder.ceval.html\">coder.ceval<\/a><\/li>\r\n         <li><a href=\"https:\/\/www.mathworks.com\/help\/simulink\/ug\/what-is-matlab-system-block.html\">MATLAB System<\/a> block using coder.ceval\r\n         <\/li>\r\n         <li>Stateflow<\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <p>As part of the examples, it also demonstrates how to load a shared library into MATLAB using the <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/loadlibrary.html\">LOADLIBRARY<\/a> function.\r\n   <\/p>\r\n   <p>And (my favorite) demonstrates how to load a library that was generated from Simulink using <a href=\"https:\/\/www.mathworks.com\/products\/embedded-coder\/\">Embedded Coder<\/a>.\r\n   <\/p>\r\n   <h3>Do people actually do this?<a name=\"3\"><\/a><\/h3>\r\n   <p>Yes, <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/?search_submit=fileexchange&amp;query=loadlibrary&amp;term=loadlibrary\">search \"loadlibrary\"<\/a> on the MATLAB File Exchange to give you an idea of how people might use this type of capability.\r\n   <\/p>\r\n   <h3>Why did I choose this entry?<a name=\"4\"><\/a><\/h3>\r\n   <p>To start, this entry is well formed and well documented.<\/p>\r\n   <p>Also for me, coming across this entry is very timely. I have been working on some projects that involve deploying Simulink\r\n      models as MEX-files. Notice that you can generate a DLL from Simulink, and then load that DLL into MATLAB. I have used that\r\n      very method, but instead of using LOADLIBRARY, I call the DLL from a MEX-file that has been compiled for MATLAB.\r\n   <\/p>\r\n   <h3>How important is this capability in Simulink?<a name=\"5\"><\/a><\/h3>\r\n   <div>\r\n      <ul>\r\n         <li>Do you need to access shared libraries from your Simulink model?<\/li>\r\n         <li>Do you want access to simulations exported from other tools?<\/li>\r\n         <li>Should C-code generation support calling a function in a DLL?<\/li>\r\n         <li>Is it useful to call Simulink models as a MEX-function?<\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <p>Let us know <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=6891#respond\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_83b049c187a64b2690c7a0a7f41176b4() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='83b049c187a64b2690c7a0a7f41176b4 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 83b049c187a64b2690c7a0a7f41176b4';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Greg Wolff';\r\n        copyright = 'Copyright 2016 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_83b049c187a64b2690c7a0a7f41176b4()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 9.0<br><\/p>\r\n<\/div>\r\n<!--\r\n83b049c187a64b2690c7a0a7f41176b4 ##### SOURCE BEGIN #####\r\n%% Sharing is Caring...So call your DLL to say \"I love you!\"\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/32620 Greg's> \r\n% pick this week is <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/44197-calling-shared-libraries-from-simulink \r\n% Calling Shared Libraries from Simulink> by <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/2945398-mikhail \r\n% Mikhail>.\r\n% \r\n% Perhaps you have a DLL that you want to access from a Simulink model.  \r\n% Maybe a colleague has developed an algorithm in another language that can be \r\n% compiled as a shared library, and you want to take advantage of it.\r\n% \r\n% How can you access it from Simulink?\r\n% \r\n% If you're familiar with Simulink, you're probably groaning as the words \r\n% \"S-Function\" cross your mind, but there are other options, which can be easier.\r\n% \r\n% Mikhail's entry that describes a variety of methods you can apply to access \r\n% the functions and variables that are exposed in the shared library from Simulink.\r\n%% What is a shared library and why would I use it?\r\n% This <http:\/\/en.wikipedia.org\/wiki\/Library_(computing)#Shared_libraries explanation> \r\n% of shared libraries covers the basics.  Shared libraries make for a relatively \r\n% portable means of sharing functionality between executables.  I say relatively \r\n% because shared libraries are specific to an operating system. On Windows they \r\n% are DLL-files and Linux they are SO-files.\r\n% \r\n% Your colleague has written some set of functions in C-code used for some \r\n% other application. If your colleague compiles these functions to be exposed \r\n% via a DLL, each of you can leverage the functions from your own applications, \r\n% without needing to share the source code. (Although you generally need to know \r\n% the interface definitions of the exposed functions)\r\n%% What are the methods available to call a shared library?\r\n% Mikhail has a very nice document that describes several methods for accessing \r\n% a shared library from Simulink. \r\n% \r\n% * C-code S-function using <https:\/\/www.mathworks.com\/help\/rtw\/ug\/legacy-code-tool-code-insertion.html \r\n% Legacy Code Tool>\r\n% * <https:\/\/www.mathworks.com\/help\/simulink\/ug\/what-is-a-matlab-function-block.html? \r\n% MATLAB Function> block using <https:\/\/www.mathworks.com\/help\/simulink\/slref\/coder.ceval.html \r\n% coder.ceval>\r\n% * <https:\/\/www.mathworks.com\/help\/simulink\/ug\/what-is-matlab-system-block.html \r\n% MATLAB System> block using coder.ceval\r\n% * Stateflow\r\n% \r\n% As part of the examples, it also demonstrates how to load a shared library \r\n% into MATLAB using the <https:\/\/www.mathworks.com\/help\/matlab\/ref\/loadlibrary.html \r\n% LOADLIBRARY> function. \r\n% \r\n% And (my favorite) demonstrates how to load a library that was generated \r\n% from Simulink using <https:\/\/www.mathworks.com\/products\/embedded-coder\/ Embedded \r\n% Coder>.\r\n%% Do people actually do this?\r\n% Yes, <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/?search_submit=fileexchange&query=loadlibrary&term=loadlibrary \r\n% search \"loadlibrary\"> on the MATLAB File Exchange to give you an idea of how \r\n% people might use this type of capability.\r\n%% Why did I choose this entry?\r\n% To start, this entry is well formed and well documented. \r\n% \r\n% Also for me, coming across this entry is very timely. I have been working \r\n% on some projects that involve enable deploying Simulink models as MEX-files.  \r\n% Notice that you can generate a DLL from Simulink, and then load that DLL into \r\n% MATLAB. I have used that very method, but instead of using LOADLIBRARY, call \r\n% the DLL from a MEX-file that has been compiled for MATLAB.\r\n%% How important is this capability in Simulink?\r\n% * Do you need to access shared libraries from your Simulink model?\r\n% * Do you want access to simulations exported from other tools? \r\n% * Should C-code generation support calling a function in a DLL?\r\n% * Is it useful to call Simulink models as a MEX-function?\r\n% \r\n% Let us know <https:\/\/blogs.mathworks.com\/pick\/ here>.\r\n##### SOURCE END ##### 83b049c187a64b2690c7a0a7f41176b4\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      Greg's pick this week is Calling Shared Libraries from Simulink by Mikhail.\r\n      \r\n      Perhaps you have a DLL that you want to access from a Simulink model. Maybe a colleague... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2016\/05\/06\/sharing-is-caring-so-call-your-dll-to-say-i-love-you\/\">read more >><\/a><\/p>","protected":false},"author":36,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/6891"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/36"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=6891"}],"version-history":[{"count":10,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/6891\/revisions"}],"predecessor-version":[{"id":8810,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/6891\/revisions\/8810"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=6891"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=6891"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=6891"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}