Go to the first, previous, next, last section, table of contents.

The ffi program

Foreign-Functions Interface

This is to announce a foreign function interface package for Objective CAML. It is designed to allow you to specify the interface to C types and data using ocaml code, which is then translated into a mix of C code and ocaml external declarations. It is patterned after the foreign function facilities of Manuel Serrano's Bigloo Scheme compiler, and it even includes a program to convert a set of Bigloo directives into an ocaml specification. Furthermore, Bigloo includes a program to convert C header files into Bigloo foreign interface directives, so the automatic generation of interfaces is within reach.

Several examples are included, one called "libc.ml" which provides access to a few libc functions and structures. It includes getenv, gettimeofday and the timeval struct, fopen, fread, fclose, and a few others. The second is x.ml, which was automatically generated from a scheme file xlib.sch, which itself was generated from the C header files for Xlib using the cigloo tool, which is part of the bigloo distribution. From this it builds xtop, a top level with access to the X library, and "hello", a translation to ocaml of a small Xlib example program borrowed from a book by Oliver Jones. (Warning, as of ocaml-3.01 the xtop program gets a segmentation fault.) The third example is in the xextra subdirectory, an X library extension written in C++ and an image viewer program "xextra-test." Left mouse button drags, middle and right buttons zoom.

There are more than a few remaining rough spots in the program. I haven't tried some of the types it purports to handle. This situation should improve in the coming weeks. This is only my second ocaml program, so I am making this preliminary release to try and get some comments and suggestions about any aspect of the design or implementation that people want to tell me about, or even whether the idea of generating FFI's this way has value. And if anyone wants to help. You may notice a few peculiar things about the program:

  1. I don't understand the module system,
  2. I don't understand signatures,
  3. I don't understand labels,

and so on and so on.

David Fox April, 2001 La Jolla, CA

Go to the first, previous, next, last section, table of contents.