From: Luca de Alfaro <luca@dealfaro.org>
To: guillaume.yziquel@citycable.ch
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Re: How to wrap around C++?
Date: Mon, 8 Feb 2010 09:57:59 -0800 [thread overview]
Message-ID: <7b0bd61a1002080957u31ec557bha59fd1d38995ebe3@mail.gmail.com> (raw)
In-Reply-To: <4B704D68.4000404@citycable.ch>
[-- Attachment #1: Type: text/plain, Size: 3729 bytes --]
Thank you Guillaume.
The reason why it is complex for me to use C/C++ inside Ocaml is that it is
a huge, and hugely complex, body of C/C++ code, that comes with all sort of
nonstandard things, and with its own entire build process. It is not clear
to me what happens if the main file is not some very special setup that
C/C++ is using. I will look into it, but the difficulty is determined by
the conventions and build process of the C/C++ I have to deal with.
Luca
On Mon, Feb 8, 2010 at 9:44 AM, Guillaume Yziquel <
guillaume.yziquel@citycable.ch> wrote:
> Luca de Alfaro a écrit :
>
>> I am trying another approach... it might make more sense for me to embed
>> the
>> Ocaml into C++.
>>
>
> This is not the way you'll get the most help out of this list. People are
> more familiar with making C bindings. Making C++ bindings is rather close to
> it.
>
>
> I have read the instructions, and it seems feasible, except that I have a
>> few questions:
>>
>> - All I need to pass, as arguments, are int, float, string, and arrays
>> 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 C
> structs and wrapping them into OCaml.
>
> The manual describes the structure of OCaml values rather precisely. There
> are also some pages by Richard Jones on his blog which explain rather nicely
> the internals of OCaml values. One advice: stick to the macros provided. Do
> not try to construct manually, say, OCaml strings on 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 = 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 still
>> 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
> 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
> easier and more documented to do it the other way round by calling C++ from
> OCaml.
>
>
> The problem I am trying to solve seems to be a can of worms from whichever
>> angle I take it...
>>
>
> No.
>
> The solution I proposed with Swig is very verbose, but it is a clean
> solution if you do it manually.
>
> You have Makefile compilation instructions to compile C++ with OCaml (the
> main issue with C++ and the extern "C" is essentially the name mangling of
> symbols provided by your C++ object files. All the rest is 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
> object and feed it back to OCaml. It may not be really clean, it lacks
> finalisers, but these last two points are stuff that you're going to have to
> deal with anyway if you're going with the OCaml/C interface. Look up "custom
> blocks" and finalisation in the OCaml manual section concerning the C
> interface.
>
> Luca
>>
>
> All the best,
>
>
> --
> Guillaume Yziquel
> http://yziquel.homelinux.org/
>
>
[-- Attachment #2: Type: text/html, Size: 5107 bytes --]
next prev parent reply other threads:[~2010-02-08 17:58 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-02-08 2:13 Luca de Alfaro
2010-02-08 4:06 ` Michael Ekstrand
2010-02-08 4:17 ` Michael Ekstrand
2010-02-08 16:03 ` [Caml-list] " Luca de Alfaro
2010-02-08 16:33 ` Luca de Alfaro
2010-02-08 17:44 ` Guillaume Yziquel
2010-02-08 17:57 ` Luca de Alfaro [this message]
2010-02-09 6:53 ` Basile STARYNKEVITCH
2010-02-08 17:54 ` Guillaume Yziquel
2010-02-09 0:25 ` Michael Ekstrand
2010-02-08 12:38 ` [Caml-list] " Guillaume Yziquel
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=7b0bd61a1002080957u31ec557bha59fd1d38995ebe3@mail.gmail.com \
--to=luca@dealfaro.org \
--cc=caml-list@inria.fr \
--cc=guillaume.yziquel@citycable.ch \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox