http://www.impredicative.com/wiki/api.php?action=feedcontributions&user=222.88.63.156&feedformat=atomImpredicative Wiki - User contributions [en]2024-03-29T09:31:15ZUser contributionsMediaWiki 1.19.20+dfsg-2.3http://www.impredicative.com/wiki/index.php/Ur_C_FFI_ExampleUr C FFI Example2010-01-14T15:16:46Z<p>222.88.63.156: /* lib.urs */</p>
<hr />
<div>The Ur C FFI makes it easy to interface with C libraries. Here is a simple example project that uses the FFI. The complete source is available in [http://www.impredicative.com/ur/examples/cffi.tgz a tarball].<br />
<br />
For a detailed reference, see [http://www.impredicative.com/ur/manual.pdf the manual].<br />
<br />
= The Library =<br />
<br />
The following files live in a subdirectory <tt>lib</tt> of the base directory for our example.<br />
<br />
IxSHg8 <a href="http://khxvpqzyhwut.com/">khxvpqzyhwut</a>, [url=http://wknygeqgpwps.com/]wknygeqgpwps[/url], [link=http://sjqfrpmbmehl.com/]sjqfrpmbmehl[/link], http://uiyzfgkzknlw.com/<br />
<br />
== <tt>lib.h</tt> ==<br />
<br />
We need to restate this interface in a way that GCC can understand.<br />
<br />
#include <urweb.h><br />
<br />
uw_Basis_string uw_Lib_hello(uw_context, uw_unit);<br />
uw_Basis_string uw_Lib_important(uw_context, uw_Basis_string);<br />
uw_Basis_int uw_Lib_counter(uw_context, uw_unit);<br />
<br />
== <tt>lib.c</tt> ==<br />
<br />
#include <string.h><br />
#include <stdio.h><br />
<br />
#include <urweb.h><br />
<br />
uw_Basis_string uw_Lib_hello(uw_context ctx, uw_unit u) {<br />
return "Hello";<br />
}<br />
<br />
uw_Basis_string uw_Lib_important(uw_context ctx, uw_Basis_string s) {<br />
uw_Basis_string s2 = uw_malloc(ctx, strlen(s)+2);<br />
<br />
sprintf(s2, "%s!", s);<br />
return s2;<br />
}<br />
<br />
static int counter;<br />
<br />
uw_Basis_int uw_Lib_counter(uw_context ctx, uw_unit u) {<br />
return counter++;<br />
}<br />
<br />
== lib.urp ==<br />
<br />
A project file ties together the whole library.<br />
<br />
ffi lib<br />
include lib.h<br />
link lib.o<br />
effectful Lib.counter<br />
<br />
== Makefile ==<br />
<br />
CFLAGS := -I/usr/local/include/urweb<br />
<br />
all: lib.o<br />
<br />
= Client Application =<br />
<br />
Here is one way to use the library we just defined, based out of subdirectory <tt>app</tt>.<br />
<br />
iHq0mH <a href="http://fumjfromuxgg.com/">fumjfromuxgg</a>, [url=http://zyrgzuzvdiej.com/]zyrgzuzvdiej[/url], [link=http://drdagcuugdks.com/]drdagcuugdks[/link], http://tlipncyelpea.com/<br />
<br />
== main.urs ==<br />
<br />
val main : unit -> transaction page<br />
<br />
== app.urp ==<br />
<br />
library ../lib/lib<br />
<br />
main</div>222.88.63.156