<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html xmlns:fn="http://www.w3.org/2005/02/xpath-functions"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link rel="stylesheet" href="../otp_doc.css" type="text/css"> <title>Erlang -- Problem Example</title> </head> <body bgcolor="white" text="#000000" link="#0000ff" vlink="#ff00ff" alink="#ff0000"><div id="container"> <script id="js" type="text/javascript" language="JavaScript" src="../js/flipmenu/flipmenu.js"></script><script id="js2" type="text/javascript" src="../js/erlresolvelinks.js"></script><script language="JavaScript" type="text/javascript"> <!-- function getWinHeight() { var myHeight = 0; if( typeof( window.innerHeight ) == 'number' ) { //Non-IE myHeight = window.innerHeight; } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) { //IE 6+ in 'standards compliant mode' myHeight = document.documentElement.clientHeight; } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) { //IE 4 compatible myHeight = document.body.clientHeight; } return myHeight; } function setscrollpos() { var objf=document.getElementById('loadscrollpos'); document.getElementById("leftnav").scrollTop = objf.offsetTop - getWinHeight()/2; } function addEvent(obj, evType, fn){ if (obj.addEventListener){ obj.addEventListener(evType, fn, true); return true; } else if (obj.attachEvent){ var r = obj.attachEvent("on"+evType, fn); return r; } else { return false; } } addEvent(window, 'load', setscrollpos); //--></script><div id="leftnav"><div class="innertube"> <img alt="Erlang logo" src="../erlang-logo.png"><br><small><a href="users_guide.html">User's Guide</a><br><a href="../pdf/otp-system-documentation-5.8.5.pdf">PDF</a><br><a href="../index.html">Top</a></small><p><strong>Interoperability Tutorial</strong><br><strong>User's Guide</strong><br><small>Version 5.8.5</small></p> <br><a href="javascript:openAllFlips()">Expand All</a><br><a href="javascript:closeAllFlips()">Contract All</a><p><small><strong>Chapters</strong></small></p> <ul class="flipMenu" imagepath="../js/flipmenu"> <li id="no" title="Introduction" expanded="false">Introduction<ul> <li><a href="introduction.html"> Top of chapter </a></li> <li title="Purpose"><a href="introduction.html#id310395">Purpose</a></li> <li title="Prerequisites"><a href="introduction.html#id308146">Prerequisites</a></li> </ul> </li> <li id="no" title="Overview" expanded="false">Overview<ul> <li><a href="overview.html"> Top of chapter </a></li> <li title="Built-In Mechanisms"><a href="overview.html#id309894">Built-In Mechanisms</a></li> <li title="C and Java Libraries"><a href="overview.html#id310459">C and Java Libraries</a></li> <li title="Standard Protocols"><a href="overview.html#id306836">Standard Protocols</a></li> <li title="IC"><a href="overview.html#id309919">IC</a></li> <li title="Old Applications"><a href="overview.html#id310397">Old Applications</a></li> </ul> </li> <li id="loadscrollpos" title="Problem Example" expanded="true">Problem Example<ul> <li><a href="example.html"> Top of chapter </a></li> <li title="Description"><a href="example.html#id308393">Description</a></li> </ul> </li> <li id="no" title="Ports" expanded="false">Ports<ul> <li><a href="c_port.html"> Top of chapter </a></li> <li title="Erlang Program"><a href="c_port.html#id303981">Erlang Program</a></li> <li title="C Program"><a href="c_port.html#id306775">C Program</a></li> <li title="Running the Example"><a href="c_port.html#id310592">Running the Example</a></li> </ul> </li> <li id="no" title="Erl_Interface" expanded="false">Erl_Interface<ul> <li><a href="erl_interface.html"> Top of chapter </a></li> <li title="Erlang Program"><a href="erl_interface.html#id309007">Erlang Program</a></li> <li title="C Program"><a href="erl_interface.html#id309116">C Program</a></li> <li title="Running the Example"><a href="erl_interface.html#id309995">Running the Example</a></li> </ul> </li> <li id="no" title="Port drivers" expanded="false">Port drivers<ul> <li><a href="c_portdriver.html"> Top of chapter </a></li> <li title="Port Drivers"><a href="c_portdriver.html#id310194">Port Drivers</a></li> <li title="Erlang Program"><a href="c_portdriver.html#id310214">Erlang Program</a></li> <li title="C Driver"><a href="c_portdriver.html#id310332">C Driver</a></li> <li title="Running the Example"><a href="c_portdriver.html#id310807">Running the Example</a></li> </ul> </li> <li id="no" title="C Nodes" expanded="false">C Nodes<ul> <li><a href="cnode.html"> Top of chapter </a></li> <li title="Erlang Program"><a href="cnode.html#id310930">Erlang Program</a></li> <li title="C Program"><a href="cnode.html#id311010">C Program</a></li> <li title="Running the Example"><a href="cnode.html#id311341">Running the Example</a></li> </ul> </li> <li id="no" title="NIFs" expanded="false">NIFs<ul> <li><a href="nif.html"> Top of chapter </a></li> <li title="NIFs"><a href="nif.html#id311644">NIFs</a></li> <li title="Erlang Program"><a href="nif.html#id311665">Erlang Program</a></li> <li title="NIF library code"><a href="nif.html#id311729">NIF library code</a></li> <li title="Running the Example"><a href="nif.html#id311830">Running the Example</a></li> </ul> </li> </ul> </div></div> <div id="content"> <div class="innertube"> <h1>3 Problem Example</h1> <h3><a name="id308393">3.1 Description</a></h3> <p>A common interoperability situation is when there exists a piece of code solving some complex problem, and we would like to incorporate this piece of code in our Erlang program. Suppose for example we have the following C functions that we would like to be able to call from Erlang.</p> <div class="example"><pre> /* complex.c */ int foo(int x) { return x+1; } int bar(int y) { return y*2; } </pre></div> <p>(For the sake of keeping the example as simple as possible, the functions are not very complicated in this case).</p> <p>Preferably we would like to able to call <span class="code">foo</span> and <span class="code">bar</span> without having to bother about them actually being C functions.</p> <div class="example"><pre> % Erlang code ... Res = complex:foo(X), ...</pre></div> <p>The communication with C is hidden in the implementation of <span class="code">complex.erl</span>. In the following chapters it is shown how this module can be implemented using the different interoperability mechanisms.</p> </div> <div class="footer"> <hr> <p>Copyright © 2000-2011 Ericsson AB. All Rights Reserved.</p> </div> </div> </div></body> </html>