Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Michael Ekstrand <michael@elehack.net>
To: caml-list@inria.fr
Subject: Re: How to wrap around C++?
Date: Mon, 08 Feb 2010 18:25:37 -0600	[thread overview]
Message-ID: <hkqa20$b9t$1@ger.gmane.org> (raw)
In-Reply-To: <7b0bd61a1002080803n6cda4bc5g9eeb8d559538598f@mail.gmail.com>

On 02/08/2010 10:03 AM, Luca de Alfaro wrote:
> Thank you very much!  I follow the general lines, but...
> 
>     * Make sure no C++ exceptions leak to OCaml.
> 
> This will be next to impossible: the C++ code I need to wrap is huge,
> and I have no idea of what possible exceptions can be generated.  I will
> have to try to see if there is a generic exception catcher. 

There is.

try {
   some code
} catch (...) {
   caml_failwith("foo: exception thrown");
}

with appropriate considerations for freeing memory, etc.

>     * Wrap your OCaml includes in 'extern "C" { ... }"
> 
> 
> Here, I am not sure what you mean.  You mean, 
> 
> extern "C" {
> #include <caml/mlvalues.h>
> ...
> }
> 

Exactly.

>     * Export all your stub functions with C linkage (extern "C")
> 
> Ok, evidently, I need to learn this extern "C" construct.

Yes, if you're interfacing between C and C++, you do need to know it.
In this context, it is applied as a modifier to the function definition
(like static).  The big thing it does is turn off the name mangling used
by the C++ compiler to implement overloading, type safety, etc.

>     * Compiling is tricky, since the OCaml compiler driver doesn't know what
>     to do with C++.  The Swig documentation[1] has a workaround for this,
>     useful even if you don't use Swig.
> 
> 
> Why would the Ocaml compiler driver need to know what to do with C++? 
> The C++ I need to link to is rather huge, and I will need to compile it
> with its own build setup. 
> Once that is built, I need to compile the stubs, the Ocaml, and link the
> three together (Ocaml, stubs, and C++), in native mode, but  why would
> the Ocaml compiler need to deal with C++?

You will have 3 pieces: the C++ library, the OCaml interface, and the
stubs gluing them together.  Stubs must be C-compatible, so they can be
called from OCaml.  But they also need to be able to call into C++,
hence they must be written in C++.  Therefore, your stub code must be
written in C++, exporting a C-compatible interface (via extern "C").
Generally, you use the OCaml compiler driver to compile stubs rather
than gcc directly, but OCaml doesn't know how to call g++.

- Michael



  parent reply	other threads:[~2010-02-09  0:26 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
2010-02-09  6:53         ` Basile STARYNKEVITCH
2010-02-08 17:54     ` Guillaume Yziquel
2010-02-09  0:25     ` Michael Ekstrand [this message]
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='hkqa20$b9t$1@ger.gmane.org' \
    --to=michael@elehack.net \
    --cc=caml-list@inria.fr \
    /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