`SWIG/Examples/ocaml/simple/`

## Simple Ocaml Example

This example illustrates how you can hook Ocaml to a very simple C program containing a function and a global variable.

## The C Code

Suppose you have the following C code:

>     /* 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;
>     }

## The SWIG interface

Here is a simple SWIG interface file:

>     /* File: example.i */
>     %module example

>     extern int gcd(int x, int y);
>     extern double Foo;

## Compilation

1. `swig -ocaml example.i`

2. Compile `example_wrap.c` and `example.c` to create the extension `examplemodule.so`.

## Using the extension

Click here to see a script that calls our C functions from Ocaml.

## Key points

- Use the `open` statement to load your extension module from Ocaml. For example:

  >     open Example

- C functions work just like Ocaml functions. For example:

  >     let g = _gcd '(x,y) as int

- C global variable Foo is wrapped as _Foo in ocaml. For example:

  >     let _ = Printf.printf "Foo = %f\n" (_Foo '() as float)