From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by yquem.inria.fr (Postfix) with ESMTP id E8DECBC37 for ; Wed, 24 Feb 2010 02:11:26 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AtEEAK8LhEtQW+UMgWdsb2JhbACbCRUBARYkIr4bhG4EgxU X-IronPort-AV: E=Sophos;i="4.49,528,1262559600"; d="scan'208";a="57693410" Received: from lo.gmane.org ([80.91.229.12]) by mail4-smtp-sop.national.inria.fr with ESMTP; 24 Feb 2010 02:11:26 +0100 Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1Nk5me-0006cY-RQ for caml-list@inria.fr; Wed, 24 Feb 2010 02:11:24 +0100 Received: from elehack.net ([216.243.177.100]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Wed, 24 Feb 2010 02:11:24 +0100 Received: from michael by elehack.net with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Wed, 24 Feb 2010 02:11:24 +0100 X-Injected-Via-Gmane: http://gmane.org/ To: caml-list@inria.fr From: Michael Ekstrand Subject: Re: How to call OCaml from C++ Date: Tue, 23 Feb 2010 19:11:11 -0600 Message-ID: References: <9dfe358d1002221911x27e70e1h7c1521b38184e372@mail.gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 8bit X-Complaints-To: usenet@dough.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: <9dfe358d1002221911x27e70e1h7c1521b38184e372@mail.gmail.com> X-Enigmail-Version: 1.0.1 X-Spam: no; 0.00; ocaml:01 ocaml:01 extern:01 alloc:01 libasmrun:01 libasmrun:01 alloc:01 extern:01 mlvalues:01 compiler:01 runtime:01 zhao:98 wrote:01 functions:01 functions:01 On 02/22/2010 09:11 PM, Jianzhou Zhao wrote: > Hi, > > Does anyone call OCaml code from C++? > C can call OCaml with the OCaml foreign interfaces. > But when we call OCaml from C++, we need to consider ‘extern C' > stuff, like what we do when call C from C++, because the bridge > functions between C and OCaml are written in C. > > However, those functions have not been prefixed with 'extern C', > and some functions, such as caml_alloc_dummy in libasmrun, have not > been exposed > in a head file. So it is hard to define a wrapper to let it work in C++. > When linking C++ and libasmrun, caml_alloc_dummy cannot > be found by the linker. If you include your OCaml includes in an 'extern C' block like this: extern "C" { #include /* other OCaml includes */ } then the C++ compiler should have the right symbol names so that it can look things up in the runtime. The headers do not have extern C declarations in them, but they are able to be imported cleanly in an extern "C" block. - Michael