[ocaml-ctypes] ANN: ocaml-ctypes 0.3

Jeremy Yallop yallop at gmail.com
Thu May 22 09:24:38 BST 2014


I'm pleased to announce the release of ocaml-ctypes 0.3, which is now
available on OPAM and for download from GitHub:

    https://github.com/ocamllabs/ocaml-ctypes/releases/

The ocaml-ctypes library makes it possible to call C functions
directly from OCaml without writing any C code.  For example, you can
bind C's "puts" function by supplying its name and type:

   # let puts = foreign "puts" (string @-> returning int);;
   val puts : string -> int = <fun>

You can then call the function immediately:

   # puts "Hello, world!";;
   Hello, world!
   - : int = 14

This is a simple example, but you can use the library to describe most
standard C types -- scalars, functions, structs, unions, arrays, and
pointers to values and functions -- making it possible to bind and
call almost any C function without leaving OCaml.

The ocaml-ctypes 0.1 release was announced on caml-list last summer:

   https://sympa.inria.fr/sympa/arc/caml-list/2013-06/msg00046.html

Since then the library has acquired a number of new features, including

 * Stub generation for functions.

   In addition to the dynamic binding mode illustrated with "puts"
above, ocaml-ctypes now supports generating stub code which can be
statically linked into your program.  The interface for stub
generation is close to the existing interface for dynamic binding, so
it should be easy to adapt existing code that uses ctypes to use stub
generation.  The pull request that introduced stub generation
describes the interface:

   https://github.com/ocamllabs/ocaml-ctypes/pull/129

   The release notes link to a number of examples:

   https://github.com/ocamllabs/ocaml-ctypes/releases/tag/ocaml-ctypes-0.3

 * Support for inverse stub generation.

   Stub generation can also operate in reverse, turning OCaml code
into a shared library that can be used from C.  There is an example
available which exposes a simple subset of Xmlm's XML-parsing API to C
(all without actually writing any C, of course):

   https://github.com/yallop/ocaml-ctypes-inverted-stubs-example/

 * Support for bigarrays.

 * Support for passing OCaml strings directly to C. (Patch by Peter Zotov.)

 * Top-level printing for C types and values: you can now build a
representation of a C type or a value and see its C printed form:

    # void @-> returning (ptr (array 10 (funptr (void @-> returning char))));;
    - : (unit -> (unit -> char) carray ptr) fn = char(*(*(void))[10])(void)

    # struct_value ;;
    - : (s, [ `Struct ]) structured = { i = 10, d = 3.5  }

The release notes for this release and for ocaml-ctypes 0.2 list a
number of other newly-available features.  There is also one
backwards-incompatible change: what was previously the Ctypes.Array
module is now called Ctypes.CArray.

I'm grateful to the following people for contributions to the 0.2 and
0.3 releases:
   Daniel Bünzli
   Ivan Gotovchits
   Rob Hoes
   Anil Madhavapeddy
   Mike McClurg
   Greg Perkins
   David Sheets
   Peter Zotov

Jeremy.


More information about the Ctypes mailing list