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 (12:36)
From: David Teller <David.Teller@e...>
Subject: Re: [Caml-list] OCaml/C interface
I'm wondering how hard it would be to write a generic module for calling
C functions without having to write a specific wrapper. Something along
the lines of (with revised syntax)

type c_type = 
 [ TVoid
 | TChar
 | TInt32
 | TInt64
 | ...
 | TStruct  of list c_type
 | TUnion   of list c_type
 | TPointer of c_type
 | TArray   of array c_type
 | TString
 | TWString
 | ...
 | TReclaimable of c_type
 | TNotReclaimable of c_type

type c_value =
 [ Void
 | TChar of char
 | TInt32 of int32
 | ...

type t; (*The type of a C function*)

 Perform a dynamic link with a C function.
 Evalutes to None in case of dynamic linking error.
value acquire_function : ~name:string -> ~args:list c_type -> option t; 

 Actually perform the call.
value call : t -> ~args:list c_type -> c_type;

value release_value : c_value -> unit;
value release_function : t -> unit;


Come to think about it, we could draw some inspiration from Python's 
CTypes [1]. While it wouldn't be as safe as the current manner of
calling C from OCaml, it would be much more convenient at least for
prototyping. Plus, with a little Camlp4, it would make it possible to
write something like

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 ?