Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
From: Christopher L Conway <cconway@c...>
Subject: Re: [Caml-list] OCaml/C interface
This is very similar to what you would get from SWIG
(http://www.swig.org/, OCaml support is in SVN): it generates a
binding where each C function has type c_obj -> c_obj, where c_obj is
more or less your c_type. Basically, this just hoists the problem up
one level of abstraction, so that instead of writing C code using
mltypes.h, you write OCaml code that massages effectively untyped
values coming out of the library.

Regards,
Chris

On 10/28/07, David Teller <David.Teller@ens-lyon.org> wrote:
> 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;
>
> Etc.
>
> 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 ?
>
> Cheers,
>  David
>
> [1] http://docs.python.org/lib/module-ctypes.html
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>
>