Skip to content

ocaml-pirates/ocaml-ctypes-inverted-stubs-example

 
 

Repository files navigation

This repository shows how to use ocaml-ctypes to turn OCaml code into a shared library that can be used from C. In keeping with the general philosophy of ocaml-ctypes, this involves writing no C code at all. The example here exposes a simple interface to the XML parsing functionality in the OCaml library Xmlm.

There are two main files involved in building the library:

  • bindings.ml uses ocaml-ctypes to define a C-compatible interface to Xmlm. The central component in the interface is a structure type that holds callbacks for each event that might occur during XML parsing. On the C side these callbacks are function pointers; on the OCaml side they appear as regular OCaml functions. In addition to the struct definition, the Bindings module exposes a single function to C, for parsing XML from a file.

  • generate.ml is an OCaml program that generates C source and header files from the definitions in the Bindings module, and an OCaml module that can be used to link the generated code with the code in Bindings. (See apply_bindings.ml for the actual linking.)

The prerequisites for building the library are OCaml 4.02.2, ocaml-ctypes (0.4.0 or later), Xmlm and ocamlfind. OPAM users can install the prerequisites by issuing the following commands:

opam update
opam switch 4.02.2
eval `opam config env`
opam install ctypes-foreign ctypes xmlm

When you type make the following things will happen:

  1. The stub generator executable will be built from bindings.ml and generate.ml.
  2. The stub generator will be run to produce a C header xmlm.h, a C source file xmlm.c, and an OCaml module xmlm_bindings.ml.
  3. The shared library will be built from the freshly-generated xmlm.c and xmlm_bindings.ml, together with bindings.ml and apply_bindings.ml.

Typing make test causes the following additional steps to take place:

  1. A test program, written in C, will be built by compiling test.c and linking it with the shared library.
  2. The test program will be run on the sample XML file ocaml.svg

Build status:

AppVeyor: AppVeyor build status
Travis: Travis build Status

About

Example showing how to turn OCaml code (here Xmlm) into a C library.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • OCaml 32.8%
  • Makefile 31.5%
  • Shell 21.2%
  • C 11.5%
  • Standard ML 3.0%