<html><head><meta http-equiv="Content-Type" content="text/html; charset=ANSI_X3.4-1968"><title>Chapter 3. Foreign Function Interface</title><link rel="stylesheet" type="text/css" href="ecl.css"><meta name="generator" content="DocBook XSL Stylesheets V1.76.1"><link rel="home" href="index.html" title="The ECL manual"><link rel="up" href="pt02.html" title="Part II. Extensions and libraries"><link rel="prev" href="ch17s03.html" title="2.3. OS Reference"><link rel="next" href="ch18s02.html" title="3.2. Two kinds of FFI"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Chapter 3. Foreign Function Interface</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch17s03.html">Prev</a> </td><th width="60%" align="center">Part II. Extensions and libraries</th><td width="20%" align="right"> <a accesskey="n" href="ch18s02.html">Next</a></td></tr></table><hr></div><div class="chapter" title="Chapter 3. Foreign Function Interface"><div class="titlepage"><div><div><h2 class="title"><a name="ext.ffi"></a>Chapter 3. Foreign Function Interface</h2></div></div></div><div class="toc"><dl><dt><span class="section"><a href="ch18.html#ext.ffi.what">3.1. What is a FFI?</a></span></dt><dt><span class="section"><a href="ch18s02.html">3.2. Two kinds of FFI</a></span></dt><dt><span class="section"><a href="ch18s03.html">3.3. Foreign objects</a></span></dt><dt><span class="section"><a href="ch18s04.html">3.4. Higher level interfaces</a></span></dt><dd><dl><dt><span class="section"><a href="ch18s04.html#ext.ffi.uffi-example">3.4.1. UFFI example</a></span></dt><dt><span class="section"><a href="ch18s04.html#ext.ffi.cffi-example">3.4.2. CFFI example</a></span></dt><dt><span class="section"><a href="ch18s04.html#ext.ffi.ecl-example">3.4.3. Low level example</a></span></dt></dl></dd><dt><span class="section"><a href="ch18s05.html">3.5. FFI Reference</a></span></dt><dd><dl><dt><span class="refentrytitle"><a href="re13.html"><code class="function">ffi:clines</code></a></span><span class="refpurpose"> — Insert C declarations and definitions</span></dt><dt><span class="refentrytitle"><a href="re14.html"><code class="function">ffi:c-inline</code></a></span><span class="refpurpose"> — Inline C code in a lisp form.</span></dt></dl></dd></dl></div><div class="section" title="3.1. What is a FFI?"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="ext.ffi.what"></a>3.1. What is a FFI?</h2></div></div></div><p>A Foreign Function Interface, or <span class="application">FFI</span> for short, is a means for a programming language to interface with libraries written in other programming languages, the <span class="emphasis"><em>foreign code</em></span>. You will see this concept most often being used in interpreted environments, such as Python, Ruby or Lisp, where one wants to reuse the big number of libraries written in C and C++ for dealing with graphical interfaces, networking, filesystems, etc.</p><p>A FFI is made of at least three components:</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><p><span class="term">Foreign objects management</span></p></td><td><p>This is the data that the foreign code will use. A <span class="application">FFI</span> needs to provide means to buid and manipulate foreign data, with automatic conversions to and from lisp data types whenever possible, and it also has to deal with issues like garbage collection and finalization.</p></td></tr><tr><td><p><span class="term">Foreign code loader</span></p></td><td><p>To actually use a foreign routine, the code must reside in memory. The process of loading this code and finding out the addresses of the routines we want to use is normally done by an independent component.</p></td></tr><tr><td><p><span class="term">Foreign function invocation</span></p></td><td><p>This is the part of the <span class="application">FFI</span> that deals with actually calling the foreign routines we want to use. For that one typically has to tell the <span class="application">FFI</span> what are the arguments that these routines expect, what are the calling conventions and where are these routines to be found.</p></td></tr></tbody></table></div><p>On top of these components sits a higher level interface written entirely in lisp, with which you will actually declare and use foreign variables, functions and libraries. In the following sections we describe both the details of the low-level components (<a class="xref" href="ch18s02.html" title="3.2. Two kinds of FFI">Section 3.2</a>, <a class="xref" href="ch18s03.html" title="3.3. Foreign objects">Section 3.3</a>), and of the higher level interface (<a class="xref" href="ch18s04.html" title="3.4. Higher level interfaces">Section 3.4</a>). It is highly recommended that you read <span class="emphasis"><em>all</em></span> sections.</p></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch17s03.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="pt02.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ch18s02.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">2.3. OS Reference </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 3.2. Two kinds of FFI</td></tr></table></div></body></html>