This example illustrates how you can hook Ocaml to a very simple C program containing a function and a global variable.
/* File : example.c */
/* A global variable */
double Foo = 3.0;
/* Compute the greatest common divisor of positive integers */
int gcd(int x, int y) {
int g;
g = y;
while (x > 0) {
g = x;
x = y % x;
y = g;
}
return g;
}
/* File: example.i */ %module example extern int gcd(int x, int y); extern double Foo;
open Example
let g = _gcd '(x,y) as int
let _ = Printf.printf "Foo = %f\n" (_Foo '() as float)