From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by yquem.inria.fr (Postfix) with ESMTP id C71B4BC37 for ; Mon, 8 Feb 2010 18:44:26 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AqQBAC7cb0tV2gB4nGdsb2JhbACDMpgOAQEBAQEICwgJE60mjySBL4JKWwSJXA X-IronPort-AV: E=Sophos;i="4.49,431,1262559600"; d="scan'208";a="51994864" Received: from emailfrontal1.citycable.ch ([85.218.0.120]) by mail1-smtp-roc.national.inria.fr with SMTP; 08 Feb 2010 18:44:26 +0100 Received: from [192.168.0.12] (unknown [85.218.92.99]) (Authenticated sender: guillaume.yziquel@citycable.ch) by emailfrontal1.citycable.ch (Postfix) with ESMTPA id 6865A12C218; Mon, 8 Feb 2010 18:44:22 +0100 (CET) Message-ID: <4B704D68.4000404@citycable.ch> Date: Mon, 08 Feb 2010 18:44:08 +0100 From: Guillaume Yziquel Reply-To: guillaume.yziquel@citycable.ch User-Agent: Mozilla-Thunderbird 2.0.0.22 (X11/20090707) MIME-Version: 1.0 To: Luca de Alfaro Cc: caml-list@inria.fr Subject: Re: [Caml-list] Re: How to wrap around C++? References: <28fa90931002071813k7330ad34s7a2ec8b4cf1c3d11@mail.gmail.com> <7b0bd61a1002080803n6cda4bc5g9eeb8d559538598f@mail.gmail.com> <28fa90931002080833y2ca71705q319a3193d3e75d9f@mail.gmail.com> In-Reply-To: <28fa90931002080833y2ca71705q319a3193d3e75d9f@mail.gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable X-Spam: no; 0.00; guillaume:01 guillaume:01 ocaml:01 bindings:01 bindings:01 arrays:01 arrays:01 structs:01 ocaml:01 alloc:01 val:01 val:01 bigarrays:01 swig:01 verbose:01 Luca de Alfaro a =C3=A9crit : > I am trying another approach... it might make more sense for me to embe= d the > Ocaml into C++. This is not the way you'll get the most help out of this list. People=20 are more familiar with making C bindings. Making C++ bindings is rather=20 close to it. > I have read the instructions, and it seems feasible, except that I have= a > few questions: >=20 > - All I need to pass, as arguments, are int, float, string, and arra= ys of > these. Any example of how to deal with the arrays? You need to construct them from C side, and it's more a pain than taking=20 C structs and wrapping them into OCaml. The manual describes the structure of OCaml values rather precisely.=20 There are also some pages by Richard Jones on his blog which explain=20 rather nicely the internals of OCaml values. One advice: stick to the=20 macros provided. Do not try to construct manually, say, OCaml strings on=20 the C side. Use caml_copy_string and friends. > - How can I return arrays, in a way that C or C++ understands? How = can I > return tuples, i.e., how can I return multiple values from Ocaml to = C? These are documented in the manual and in Richard Jones' blog. For couples, you can do > value couple =3D caml_alloc(2, 0); > Store_field(couple, 0, my_ocaml_val); > Store_field(couple, 1, my_other_ocml_val); For arrays, you'll have seamless integration by using Bigarrays. > - Finally, do I need to worry about the Ocaml garbage collector, if = I > call Ocaml from C/C++? Will it run every now and then? How can the = garbage > collector know whether a value returned by an Ocaml function is stil= l being > used in C/C++? How can I tell it that it is no longer used? Essentially, the garbage collector will run potentially each time you=20 allocate an OCaml value. caml_copy_string? the GC may run. You have to register values being used on the C side as a GC root. It's=20 easier and more documented to do it the other way round by calling C++=20 from OCaml. > The problem I am trying to solve seems to be a can of worms from whiche= ver > angle I take it... No. The solution I proposed with Swig is very verbose, but it is a clean=20 solution if you do it manually. You have Makefile compilation instructions to compile C++ with OCaml=20 (the main issue with C++ and the extern "C" is essentially the name=20 mangling of symbols provided by your C++ object files. All the rest is=20 pretty similar to C. This is really the *main* point). You have at the end of my last email and example of how to construct an=20 object and feed it back to OCaml. It may not be really clean, it lacks=20 finalisers, but these last two points are stuff that you're going to=20 have to deal with anyway if you're going with the OCaml/C interface.=20 Look up "custom blocks" and finalisation in the OCaml manual section=20 concerning the C interface. > Luca All the best, --=20 Guillaume Yziquel http://yziquel.homelinux.org/