<!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>CallingFromSMLToC</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>CallingFromSMLToC</h1>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="paragraph">
<p>MLton&#8217;s <a href="ForeignFunctionInterface">ForeignFunctionInterface</a> allows an SML program to <em>import</em>
C functions.  Suppose you would like to import from C a function with
the following prototype:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="c">int foo (double d, char c);</code></pre>
</div>
</div>
<div class="paragraph">
<p>MLton extends the syntax of SML to allow expressions like the following:</p>
</div>
<div class="listingblock">
<div class="content">
<pre>_import "foo": real * char -&gt; int;</pre>
</div>
</div>
<div class="paragraph">
<p>This expression denotes a function of type <code>real * char -&gt; int</code> whose
behavior is implemented by calling the C function whose name is <code>foo</code>.
Thinking in terms of C, imagine that there are C variables <code>d</code> of type
<code>double</code>, <code>c</code> of type <code>unsigned char</code>, and <code>i</code> of type <code>int</code>.  Then,
the C statement <code>i = foo (d, c)</code> is executed and <code>i</code> is returned.</p>
</div>
<div class="paragraph">
<p>The general form of an <code>_import</code> expression is:</p>
</div>
<div class="listingblock">
<div class="content">
<pre>_import "C function name" attr... : cFuncTy;</pre>
</div>
</div>
<div class="paragraph">
<p>The type and the semicolon are not optional.</p>
</div>
<div class="paragraph">
<p>The function name is followed by a (possibly empty) sequence of
attributes, analogous to C <code><em>attribute</em></code> specifiers.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_example">Example</h2>
<div class="sectionbody">
<div class="paragraph">
<p><code>import.sml</code> imports the C function <code>ffi</code> and the C variable <code>FFI_INT</code>
as follows.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="sml">Unresolved directive in CallingFromSMLToC.adoc - include::https://raw.github.com/MLton/mlton/master/doc/examples/ffi/import.sml[indent=0]</code></pre>
</div>
</div>
<div class="paragraph">
<p><code>ffi-import.c</code> is</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="c">Unresolved directive in CallingFromSMLToC.adoc - include::https://raw.github.com/MLton/mlton/master/doc/examples/ffi/ffi-import.c[indent=0]</code></pre>
</div>
</div>
<div class="paragraph">
<p>Compile and run the program.</p>
</div>
<div class="listingblock">
<div class="content">
<pre>% mlton -default-ann 'allowFFI true' -export-header export.h  import.sml ffi-import.c
% ./import
13
success</pre>
</div>
</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/import.sml"><code>import.sml</code></a></p>
</li>
<li>
<p><a href="https://raw.github.com/MLton/mlton/master/doc/examples/ffi/ffi-import.c"><code>ffi-import.c</code></a></p>
</li>
</ul>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_next_steps">Next Steps</h2>
<div class="sectionbody">
<div class="ulist">
<ul>
<li>
<p><a href="CallingFromSMLToCFunctionPointer">CallingFromSMLToCFunctionPointer</a></p>
</li>
</ul>
</div>
</div>
</div>
</div>
</body>
</html>