Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
OCaml/C interface
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2007-10-28 (13:34)
From: skaller <skaller@u...>
Subject: Re: [Caml-list] OCaml/C interface

On Sun, 2007-10-28 at 13:36 +0100, David Teller wrote:

> external "C" my_native_function = "char* stuff(char* a, char* b)" 
> That was just a random thought, I have no particular interest in
> implementing this, but what do you think about it ?

This is the basis of how Felix binds to C/C++.

You need to provide ways to bind *types* as well.

With experience, you'll find various property annotations
are useful.

Felix says stuff like:

	type mytype = "mytype";
	fun f: int * mytype-> int = "f($1,$2)";

and emits appropriate code. Camlp4 can do the same thing
probably, and emit C glue.

Note your form of specification isn't as good as mine.
With coding like my 'f' you specify the name and Ocaml
calling convention, then the way to wrap an Ocaml call
to a C call. In particular you could write:

	fun f: int * mytype-> int = "g($1,100,$2)+1";

The problem with your notation is that it is a bit naive:
it assumes a simple correspondence between Ocaml and C calls
that doesn't exist in general: you need to use a richer
binding language.

The actual C code would make a function which is called
which does conversions based on type stuff for the
arguments $1 $2 etc, and then actually emit the C
code with replacements of the converted values.

You need to allow for more than one kind of conversion.
Concrete copying, as in integers, is not the same as a pointer
based representation (abstract type). It's important not
to fix one or the other but allow choices.

Felix also does this:

	fun f: ... = " .. " requires '#include "mine.h"';

so that you can put the header file etc in with the 

Once you use automated wrapping, it HAS to do everything
because you can no longer intervene -- the only alternative
is write your C glue by hand in a separate file, which is
precisely what the facility here is trying to avoid.

A good design goal here is .. GET RID OF int64 etc type
from Ocaml. Work on the binding feature until it can
be done by the user with no external C code.

John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: