<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="Asciidoctor 2.0.26">
<title>CallingFromSMLToCFunctionPointer</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:300,300italic,400,400italic,600,600italic%7CNoto+Serif:400,400italic,700,700italic%7CDroid+Sans+Mono:400,700">
<link rel="stylesheet" href="./asciidoctor.css">
<link rel="stylesheet" href="./mlton.css">

</head>
<body class="article">
<div id="mlton-header">
<div id="mlton-header-text">
<h2>
<a href="./Home">
MLton
20241230+git20251029+dfsg-5
</a>
</h2>
</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">
<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">
<pre class="rouge highlight"><code data-lang="sml">MLton.Pointer.t -&gt; real * char -&gt; int</code></pre>
</div>
</div>
<div class="paragraph">
<p>whose behavior is implemented by calling the C function at the address
denoted by the <code>MLton.Pointer.t</code> argument, and supplying the C
function two arguments, a <code>double</code> and an <code>int</code>.  The C function
pointer may be obtained, for example, by the dynamic linking loader
(<code>dlopen</code>, <code>dlsym</code>, &#8230;&#8203;).</p>
</div>
<div class="paragraph">
<p>The general form of an indirect <code>_import</code> expression is:</p>
</div>
<div class="listingblock">
<div class="content">
<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 <code>dlopen</code> and friends (imported using normal
<code>_import</code>) to dynamically load the math library (<code>libm</code>) and call the
<code>cos</code> function. Suppose <code>iimport.sml</code> contains the following.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="sml">Unresolved directive in CallingFromSMLToCFunctionPointer.adoc - include::https://raw.github.com/MLton/mlton/master/doc/examples/ffi/iimport.sml[indent=0]</code></pre>
</div>
</div>
<div class="paragraph">
<p>Compile and run <code>iimport.sml</code>.</p>
</div>
<div class="listingblock">
<div class="content">
<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 <code>-target-link-opt</code> option, which uses the
switch when linking only when on the specified platform.  Compile with
<code>-verbose 1</code> to see in more detail what&#8217;s being passed to <code>gcc</code>.</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"><code>iimport.sml</code></a></p>
</li>
</ul>
</div>
</div>
</div>
</div>
</body>
</html>