OCaml has FFI for interacting with C code . The binding code must be written in C, not in OCaml (which does not have a direct representation of C values, but C has representations of OCaml values). My advice:
- On the C side, decide what the best export interface will be for C programmers (or Python programmers writing Python bindings starting from your C interface)
- Define a "low level" on the OCaml side that gets the OCaml value as close as possible to the C view
- Write some C wrappers to convert from this low-level OCaml view to your optimal C view
The reason for step (2) is to make step (3) as small as possible. Manipulating OCaml values ββfrom the C side is a bit painful, in particular, you run the risk of mistakenly interacting with the garbage collector, which means segfaults - plus you don't get any type security. So, the less work you have to do on the C side, the better.
There are several projects that can help you do some of these jobs. CamlIDL , and I think Swig has some OCaml support. I never used them, so I can not comment.
If you know which high-level language you want to convert your interface to, there may be a specialized bridge that does not require a C-step. For example, there are libraries for interacting directly with Python views (Pycaml search, not sure how they were tested) or with the Java runtime ( OCamlJava ). The C interface is still a safe bet that will allow other people to create bridges in their languages.
source share