From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by yquem.inria.fr (Postfix) with ESMTP id E0077BC37 for ; Tue, 9 Feb 2010 01:26:02 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: As8DAPk5cEtQW+UMgWdsb2JhbACbRAEBFiS7W4RUBIMr X-IronPort-AV: E=Sophos;i="4.49,432,1262559600"; d="scan'208";a="44169297" Received: from lo.gmane.org ([80.91.229.12]) by mail3-smtp-sop.national.inria.fr with ESMTP; 09 Feb 2010 01:26:02 +0100 Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1NedvR-0004pd-LV for caml-list@inria.fr; Tue, 09 Feb 2010 01:25:57 +0100 Received: from elehack.net ([216.243.177.100]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 09 Feb 2010 01:25:57 +0100 Received: from michael by elehack.net with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 09 Feb 2010 01:25:57 +0100 X-Injected-Via-Gmane: http://gmane.org/ To: caml-list@inria.fr From: Michael Ekstrand Subject: Re: How to wrap around C++? Date: Mon, 08 Feb 2010 18:25:37 -0600 Message-ID: References: <28fa90931002071813k7330ad34s7a2ec8b4cf1c3d11@mail.gmail.com> <7b0bd61a1002080803n6cda4bc5g9eeb8d559538598f@mail.gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: elehack.net User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.7) Gecko/20100111 Lightning/1.0b1 Thunderbird/3.0.1 In-Reply-To: <7b0bd61a1002080803n6cda4bc5g9eeb8d559538598f@mail.gmail.com> X-Enigmail-Version: 1.0.1 Sender: news X-Spam: no; 0.00; ocaml:01 failwith:01 foo:01 ocaml:01 extern:01 mlvalues:01 stub:01 extern:01 evidently:01 interfacing:01 mangling:01 compiler:01 overloading:01 compiler:01 swig:01 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 > ... > } > 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