<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="generator" content="AsciiDoc 8.6.10">
<title>CallingFromSMLToCFunctionPointer</title>
<link rel="stylesheet" href="./asciidoc.css" type="text/css">
<link rel="stylesheet" href="./pygments.css" type="text/css">


<script type="text/javascript" src="./asciidoc.js"></script>
<script type="text/javascript">
/*<![CDATA[*/
asciidoc.install();
/*]]>*/
</script>
<link rel="stylesheet" href="./mlton.css" type="text/css">
</head>
<body class="article">
<div id="banner">
<div id="banner-home">
<a href="./Home">MLton 20201002</a>
</div>
</div>
<div id="header">
<h1>CallingFromSMLToCFunctionPointer</h1>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="paragraph"><p>Just as MLton can <a href="CallingFromSMLToC">directly call C functions</a>, it
is possible to make indirect function calls; that is, function calls
through a function pointer.  MLton extends the syntax of SML to allow
expressions like the following:</p></div>
<div class="listingblock">
<div class="content monospaced">
<pre>_import * : MLton.Pointer.t -&gt; real * char -&gt; int;</pre>
</div></div>
<div class="paragraph"><p>This expression denotes a function of type</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="n">MLton</span><span class="p">.</span><span class="n">Pointer</span><span class="p">.</span><span class="n">t</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">real</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">char</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">int</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>whose behavior is implemented by calling the C function at the address
denoted by the <span class="monospaced">MLton.Pointer.t</span> argument, and supplying the C
function two arguments, a <span class="monospaced">double</span> and an <span class="monospaced">int</span>.  The C function
pointer may be obtained, for example, by the dynamic linking loader
(<span class="monospaced">dlopen</span>, <span class="monospaced">dlsym</span>, &#8230;).</p></div>
<div class="paragraph"><p>The general form of an indirect <span class="monospaced">_import</span> expression is:</p></div>
<div class="listingblock">
<div class="content monospaced">
<pre>_import * attr... : cPtrTy -&gt; cFuncTy;</pre>
</div></div>
<div class="paragraph"><p>The type and the semicolon are not optional.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_example">Example</h2>
<div class="sectionbody">
<div class="paragraph"><p>This example uses <span class="monospaced">dlopen</span> and friends (imported using normal
<span class="monospaced">_import</span>) to dynamically load the math library (<span class="monospaced">libm</span>) and call the
<span class="monospaced">cos</span> function. Suppose <span class="monospaced">iimport.sml</span> contains the following.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="k">signature</span><span class="w"> </span><span class="n">DYN_LINK</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">   </span><span class="k">sig</span><span class="w"></span>
<span class="w">      </span><span class="k">type</span><span class="w"> </span><span class="n">hndl</span><span class="w"></span>
<span class="w">      </span><span class="k">type</span><span class="w"> </span><span class="n">mode</span><span class="w"></span>
<span class="w">      </span><span class="k">type</span><span class="w"> </span><span class="n">fptr</span><span class="w"></span>

<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">dlopen</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">string</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">mode</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">hndl</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">dlsym</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">hndl</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">string</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">fptr</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">dlclose</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">hndl</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">unit</span><span class="w"></span>

<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">RTLD_LAZY</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">mode</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">RTLD_NOW</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">mode</span><span class="w"></span>
<span class="w">   </span><span class="k">end</span><span class="w"></span>

<span class="k">structure</span><span class="w"> </span><span class="n">DynLink</span><span class="w"> </span><span class="p">:&gt;</span><span class="w"> </span><span class="n">DYN_LINK</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">   </span><span class="k">struct</span><span class="w"></span>
<span class="w">      </span><span class="k">type</span><span class="w"> </span><span class="n">hndl</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">MLton</span><span class="p">.</span><span class="n">Pointer</span><span class="p">.</span><span class="n">t</span><span class="w"></span>
<span class="w">      </span><span class="k">type</span><span class="w"> </span><span class="n">mode</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">Word32</span><span class="p">.</span><span class="n">word</span><span class="w"></span>
<span class="w">      </span><span class="k">type</span><span class="w"> </span><span class="n">fptr</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">MLton</span><span class="p">.</span><span class="n">Pointer</span><span class="p">.</span><span class="n">t</span><span class="w"></span>

<span class="w">      </span><span class="cm">(* These symbols come from a system libray, so the default import scope</span>
<span class="cm">       * of external is correct.</span>
<span class="cm">       *)</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">dlopen</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">         </span><span class="p">_</span><span class="n">import</span><span class="w"> </span><span class="s">&quot;dlopen&quot;</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">string</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">mode</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">hndl</span><span class="p">;</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">dlerror</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">         </span><span class="p">_</span><span class="n">import</span><span class="w"> </span><span class="s">&quot;dlerror&quot;</span><span class="p">:</span><span class="w"> </span><span class="n">unit</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">MLton</span><span class="p">.</span><span class="n">Pointer</span><span class="p">.</span><span class="n">t</span><span class="p">;</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">dlsym</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">         </span><span class="p">_</span><span class="n">import</span><span class="w"> </span><span class="s">&quot;dlsym&quot;</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">hndl</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">string</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">fptr</span><span class="p">;</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">dlclose</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">         </span><span class="p">_</span><span class="n">import</span><span class="w"> </span><span class="s">&quot;dlclose&quot;</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">hndl</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">Int32</span><span class="p">.</span><span class="n">int</span><span class="p">;</span><span class="w"></span>

<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">RTLD_LAZY</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mh">0wx00001</span><span class="w"> </span><span class="cm">(* Lazy function call binding.  *)</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">RTLD_NOW</span><span class="w">  </span><span class="p">=</span><span class="w"> </span><span class="mh">0wx00002</span><span class="w"> </span><span class="cm">(* Immediate function call binding.  *)</span><span class="w"></span>

<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">dlerror</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="k">fn</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"></span>
<span class="w">         </span><span class="k">let</span><span class="w"></span>
<span class="w">            </span><span class="k">val</span><span class="w"> </span><span class="n">addr</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">dlerror</span><span class="w"> </span><span class="p">()</span><span class="w"></span>
<span class="w">         </span><span class="k">in</span><span class="w"></span>
<span class="w">            </span><span class="k">if</span><span class="w"> </span><span class="n">addr</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">MLton</span><span class="p">.</span><span class="n">Pointer</span><span class="p">.</span><span class="n">null</span><span class="w"></span>
<span class="w">               </span><span class="k">then</span><span class="w"> </span><span class="n">NONE</span><span class="w"></span>
<span class="w">               </span><span class="k">else</span><span class="w"> </span><span class="k">let</span><span class="w"></span>
<span class="w">                       </span><span class="k">fun</span><span class="w"> </span><span class="n">loop</span><span class="w"> </span><span class="p">(</span><span class="n">index</span><span class="p">,</span><span class="w"> </span><span class="n">cs</span><span class="p">)</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">                          </span><span class="k">let</span><span class="w"></span>
<span class="w">                             </span><span class="k">val</span><span class="w"> </span><span class="n">w</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">MLton</span><span class="p">.</span><span class="n">Pointer</span><span class="p">.</span><span class="n">getWord8</span><span class="w"> </span><span class="p">(</span><span class="n">addr</span><span class="p">,</span><span class="w"> </span><span class="n">index</span><span class="p">)</span><span class="w"></span>
<span class="w">                             </span><span class="k">val</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">Byte</span><span class="p">.</span><span class="n">byteToChar</span><span class="w"> </span><span class="n">w</span><span class="w"></span>
<span class="w">                          </span><span class="k">in</span><span class="w"></span>
<span class="w">                             </span><span class="k">if</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">#</span><span class="s">&quot;</span><span class="se">\000</span><span class="s">&quot;</span><span class="w"></span>
<span class="w">                                </span><span class="k">then</span><span class="w"> </span><span class="n">SOME</span><span class="w"> </span><span class="p">(</span><span class="n">implode</span><span class="w"> </span><span class="p">(</span><span class="n">rev</span><span class="w"> </span><span class="n">cs</span><span class="p">))</span><span class="w"></span>
<span class="w">                                </span><span class="k">else</span><span class="w"> </span><span class="n">loop</span><span class="w"> </span><span class="p">(</span><span class="n">index</span><span class="w"> </span><span class="n">+</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="n">c::cs</span><span class="p">)</span><span class="w"></span>
<span class="w">                          </span><span class="k">end</span><span class="w"></span>
<span class="w">                    </span><span class="k">in</span><span class="w"></span>
<span class="w">                       </span><span class="n">loop</span><span class="w"> </span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="p">[])</span><span class="w"></span>
<span class="w">                    </span><span class="k">end</span><span class="w"></span>
<span class="w">         </span><span class="k">end</span><span class="w"></span>

<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">dlopen</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="k">fn</span><span class="w"> </span><span class="p">(</span><span class="n">filename</span><span class="p">,</span><span class="w"> </span><span class="n">mode</span><span class="p">)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"></span>
<span class="w">         </span><span class="k">let</span><span class="w"></span>
<span class="w">            </span><span class="k">val</span><span class="w"> </span><span class="n">filename</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">filename</span><span class="w"> </span><span class="n">^</span><span class="w"> </span><span class="s">&quot;</span><span class="se">\000</span><span class="s">&quot;</span><span class="w"></span>
<span class="w">            </span><span class="k">val</span><span class="w"> </span><span class="n">hndl</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">dlopen</span><span class="w"> </span><span class="p">(</span><span class="n">filename</span><span class="p">,</span><span class="w"> </span><span class="n">mode</span><span class="p">)</span><span class="w"></span>
<span class="w">         </span><span class="k">in</span><span class="w"></span>
<span class="w">            </span><span class="k">if</span><span class="w"> </span><span class="n">hndl</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">MLton</span><span class="p">.</span><span class="n">Pointer</span><span class="p">.</span><span class="n">null</span><span class="w"></span>
<span class="w">               </span><span class="k">then</span><span class="w"> </span><span class="k">raise</span><span class="w"> </span><span class="n">Fail</span><span class="w"> </span><span class="p">(</span><span class="k">case</span><span class="w"> </span><span class="n">dlerror</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="k">of</span><span class="w"></span>
<span class="w">                                   </span><span class="n">NONE</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="s">&quot;???&quot;</span><span class="w"></span>
<span class="w">                                 </span><span class="p">|</span><span class="w"> </span><span class="n">SOME</span><span class="w"> </span><span class="n">s</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">s</span><span class="p">)</span><span class="w"></span>
<span class="w">               </span><span class="k">else</span><span class="w"> </span><span class="n">hndl</span><span class="w"></span>
<span class="w">         </span><span class="k">end</span><span class="w"></span>

<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">dlsym</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="k">fn</span><span class="w"> </span><span class="p">(</span><span class="n">hndl</span><span class="p">,</span><span class="w"> </span><span class="n">symbol</span><span class="p">)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"></span>
<span class="w">         </span><span class="k">let</span><span class="w"></span>
<span class="w">            </span><span class="k">val</span><span class="w"> </span><span class="n">symbol</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">symbol</span><span class="w"> </span><span class="n">^</span><span class="w"> </span><span class="s">&quot;</span><span class="se">\000</span><span class="s">&quot;</span><span class="w"></span>
<span class="w">            </span><span class="k">val</span><span class="w"> </span><span class="n">fptr</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">dlsym</span><span class="w"> </span><span class="p">(</span><span class="n">hndl</span><span class="p">,</span><span class="w"> </span><span class="n">symbol</span><span class="p">)</span><span class="w"></span>
<span class="w">         </span><span class="k">in</span><span class="w"></span>
<span class="w">            </span><span class="k">case</span><span class="w"> </span><span class="n">dlerror</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="k">of</span><span class="w"></span>
<span class="w">               </span><span class="n">NONE</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">fptr</span><span class="w"></span>
<span class="w">             </span><span class="p">|</span><span class="w"> </span><span class="n">SOME</span><span class="w"> </span><span class="n">s</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="k">raise</span><span class="w"> </span><span class="n">Fail</span><span class="w"> </span><span class="n">s</span><span class="w"></span>
<span class="w">         </span><span class="k">end</span><span class="w"></span>

<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">dlclose</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="k">fn</span><span class="w"> </span><span class="n">hndl</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"></span>
<span class="w">         </span><span class="k">if</span><span class="w"> </span><span class="n">MLton</span><span class="p">.</span><span class="n">Platform</span><span class="p">.</span><span class="n">OS</span><span class="p">.</span><span class="n">host</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">MLton</span><span class="p">.</span><span class="n">Platform</span><span class="p">.</span><span class="n">OS</span><span class="p">.</span><span class="n">Darwin</span><span class="w"></span>
<span class="w">            </span><span class="k">then</span><span class="w"> </span><span class="p">()</span><span class="w">  </span><span class="cm">(* Darwin reports the following error message if you</span>
<span class="cm">                      * try to close a dynamic library.</span>
<span class="cm">                      *   &quot;dynamic libraries cannot be closed&quot;</span>
<span class="cm">                      * So, we disable dlclose on Darwin.</span>
<span class="cm">                      *)</span><span class="w"></span>
<span class="w">         </span><span class="k">else</span><span class="w"></span>
<span class="w">            </span><span class="k">let</span><span class="w"></span>
<span class="w">               </span><span class="k">val</span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">dlclose</span><span class="w"> </span><span class="n">hndl</span><span class="w"></span>
<span class="w">            </span><span class="k">in</span><span class="w"></span>
<span class="w">               </span><span class="k">if</span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">0</span><span class="w"></span>
<span class="w">                  </span><span class="k">then</span><span class="w"> </span><span class="p">()</span><span class="w"></span>
<span class="w">               </span><span class="k">else</span><span class="w"> </span><span class="k">raise</span><span class="w"> </span><span class="n">Fail</span><span class="w"> </span><span class="p">(</span><span class="k">case</span><span class="w"> </span><span class="n">dlerror</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="k">of</span><span class="w"></span>
<span class="w">                                   </span><span class="n">NONE</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="s">&quot;???&quot;</span><span class="w"></span>
<span class="w">                                 </span><span class="p">|</span><span class="w"> </span><span class="n">SOME</span><span class="w"> </span><span class="n">s</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">s</span><span class="p">)</span><span class="w"></span>
<span class="w">            </span><span class="k">end</span><span class="w"></span>
<span class="w">   </span><span class="k">end</span><span class="w"></span>

<span class="k">val</span><span class="w"> </span><span class="n">dll</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">   </span><span class="k">let</span><span class="w"></span>
<span class="w">      </span><span class="k">open</span><span class="w"> </span><span class="n">MLton</span><span class="p">.</span><span class="n">Platform</span><span class="p">.</span><span class="n">OS</span><span class="w"></span>
<span class="w">   </span><span class="k">in</span><span class="w"></span>
<span class="w">      </span><span class="k">case</span><span class="w"> </span><span class="n">host</span><span class="w"> </span><span class="k">of</span><span class="w"></span>
<span class="w">         </span><span class="n">Cygwin</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="s">&quot;cygwin1.dll&quot;</span><span class="w"></span>
<span class="w">       </span><span class="p">|</span><span class="w"> </span><span class="n">Darwin</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="s">&quot;libm.dylib&quot;</span><span class="w"></span>
<span class="w">       </span><span class="p">|</span><span class="w"> </span><span class="p">_</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="s">&quot;libm.so&quot;</span><span class="w"></span>
<span class="w">   </span><span class="k">end</span><span class="w"></span>

<span class="k">val</span><span class="w"> </span><span class="n">hndl</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">DynLink</span><span class="p">.</span><span class="n">dlopen</span><span class="w"> </span><span class="p">(</span><span class="n">dll</span><span class="p">,</span><span class="w"> </span><span class="n">DynLink</span><span class="p">.</span><span class="n">RTLD_LAZY</span><span class="p">)</span><span class="w"></span>

<span class="k">local</span><span class="w"></span>
<span class="w">   </span><span class="k">val</span><span class="w"> </span><span class="n">double_to_double</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">      </span><span class="p">_</span><span class="n">import</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">DynLink</span><span class="p">.</span><span class="n">fptr</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">real</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">real</span><span class="p">;</span><span class="w"></span>
<span class="w">   </span><span class="k">val</span><span class="w"> </span><span class="n">cos_fptr</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">DynLink</span><span class="p">.</span><span class="n">dlsym</span><span class="w"> </span><span class="p">(</span><span class="n">hndl</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;cos&quot;</span><span class="p">)</span><span class="w"></span>
<span class="k">in</span><span class="w"></span>
<span class="w">   </span><span class="k">val</span><span class="w"> </span><span class="n">cos</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">double_to_double</span><span class="w"> </span><span class="n">cos_fptr</span><span class="w"></span>
<span class="k">end</span><span class="w"></span>

<span class="k">val</span><span class="w"> </span><span class="p">_</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">print</span><span class="w"> </span><span class="p">(</span><span class="n">concat</span><span class="w"> </span><span class="p">[</span><span class="s">&quot;    Math.cos(2.0) = &quot;</span><span class="p">,</span><span class="w"> </span><span class="n">Real</span><span class="p">.</span><span class="n">toString</span><span class="w"> </span><span class="p">(</span><span class="n">Math</span><span class="p">.</span><span class="n">cos</span><span class="w"> </span><span class="mf">2.0</span><span class="p">),</span><span class="w"> </span><span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"></span>
<span class="w">                       </span><span class="s">&quot;libm.so::cos(2.0) = &quot;</span><span class="p">,</span><span class="w"> </span><span class="n">Real</span><span class="p">.</span><span class="n">toString</span><span class="w"> </span><span class="p">(</span><span class="n">cos</span><span class="w"> </span><span class="mf">2.0</span><span class="p">),</span><span class="w"> </span><span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">])</span><span class="w"></span>

<span class="k">val</span><span class="w"> </span><span class="p">_</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">DynLink</span><span class="p">.</span><span class="n">dlclose</span><span class="w"> </span><span class="n">hndl</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>Compile and run <span class="monospaced">iimport.sml</span>.</p></div>
<div class="listingblock">
<div class="content monospaced">
<pre>% mlton -default-ann 'allowFFI true'    \
        -target-link-opt linux -ldl     \
        -target-link-opt solaris -ldl   \
         iimport.sml
% iimport
    Math.cos(2.0) = ~0.416146836547
libm.so::cos(2.0) = ~0.416146836547</pre>
</div></div>
<div class="paragraph"><p>This example also shows the <span class="monospaced">-target-link-opt</span> option, which uses the
switch when linking only when on the specified platform.  Compile with
<span class="monospaced">-verbose 1</span> to see in more detail what&#8217;s being passed to <span class="monospaced">gcc</span>.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_download">Download</h2>
<div class="sectionbody">
<div class="ulist"><ul>
<li>
<p>
<a href="https://raw.github.com/MLton/mlton/master/doc/examples/ffi/iimport.sml"><span class="monospaced">iimport.sml</span></a>
</p>
</li>
</ul></div>
</div>
</div>
</div>
<div id="footnotes"><hr></div>
<div id="footer">
<div id="footer-text">
</div>
<div id="footer-badges">
</div>
</div>
</body>
</html>
