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 7D261BC37 for ; Mon, 8 Feb 2010 18:54:50 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AggEAIfeb0tV2gB5gWdsb2JhbACDMpgOAQEWJK0mjyiBL4JKWwSDK4Yx X-IronPort-AV: E=Sophos;i="4.49,431,1262559600"; d="scan'208";a="44121163" Received: from emailfrontal2.citycable.ch ([85.218.0.121]) by mail3-smtp-sop.national.inria.fr with SMTP; 08 Feb 2010 18:54:50 +0100 Received: from [192.168.0.12] (unknown [85.218.92.99]) (Authenticated sender: guillaume.yziquel@citycable.ch) by emailfrontal2.citycable.ch (Postfix) with ESMTPA id C02FF834431; Mon, 8 Feb 2010 18:54:43 +0100 (CET) Message-ID: <4B704FD0.7010104@citycable.ch> Date: Mon, 08 Feb 2010 18:54:24 +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: Michael Ekstrand , caml-list@inria.fr Subject: Re: [Caml-list] Re: How to wrap around C++? References: <28fa90931002071813k7330ad34s7a2ec8b4cf1c3d11@mail.gmail.com> <7b0bd61a1002080803n6cda4bc5g9eeb8d559538598f@mail.gmail.com> In-Reply-To: <7b0bd61a1002080803n6cda4bc5g9eeb8d559538598f@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 extern:01 mlvalues:01 ocaml:01 extern:01 alloc:01 mlvalues:01 camlparam:01 swig:01 val:01 val:01 const:01 alloc:01 Luca de Alfaro a =C3=A9crit : > Thank you very much! I follow the general lines, but... >=20 >> * Wrap your OCaml includes in 'extern "C" { ... }" >=20 > Here, I am not sure what you mean. You mean, >=20 > extern "C" { > #include > ... > } >=20 > ? See the code in my email: > /* Including OCaml system. */ > #define CAML_VALUE value > extern "C" { > #include > #include > #include > #include > #include > #include > #include > } on one hand, and > extern "C" CAML_VALUE _wrap_tokenizer_tokenize (CAML_VALUE ocaml_arg1, = CAML_VALUE ocaml_arg2) > { > CAMLparam0(); > SWIG_CAMLlocal1(caml_result); > tokenizer *arg1 =3D (tokenizer *) 0 ; > std::string *arg2 =3D 0 ; > CAMLxparam1(ocaml_arg1); > CAMLxparam1(ocaml_arg2); > std::list< word > result; > =20 > arg1 =3D *((tokenizer * *) Data_custom_val(ocaml_arg1));=20 > { > std::string arg2_str(String_val(ocaml_arg2), caml_string_length(oca= ml_arg2)); > arg2 =3D &arg2_str; > } > result =3D (arg1)->tokenize((std::string const &)*arg2); > { > caml_result =3D caml_alloc_custom(&custom_swigtype_ocaml_operations= , sizeof (void *), 0 ,1); > *((void **) Data_custom_val(caml_result)) =3D new std::list< word >= ((const std::list< word > &)result); > } > CAMLreturn(caml_result); > } on the other... >> * Export all your stub functions with C linkage (extern "C") >=20 > Ok, evidently, I need to learn this extern "C" construct. Yes. It's essentially here to cope with C++ name mangling. >> * Compiling is tricky, since the OCaml compiler driver doesn't know wh= at >> 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++? Because it knows how to compile C, but doesn't know how to compile C++. > The C++ I need to link to is rather huge, and I will need to compile it= with > its own build setup. Compile your C++ on one hand as you usually would. No issue there. You=20 could even wrap a .so file generated from C++ code without knowing=20 anything about the source code of the .so file. OK, except headers, and=20 vendor info and version info of your C++ compiler. But, you have to compile the C glue as above with extern "C" and the=20 trick I gave you in the Makefile, which btw come from the Swig website. > Once that is built, I need to compile the stubs, the Ocaml, and link th= e > three together (Ocaml, stubs, and C++), in native mode, but why would = the > Ocaml compiler need to deal with C++? The OCaml compiler does not deal with C++. It only knows about C. It's the C stub that need to: -1- be linked in OCaml code, and therefore, no C++ name mangling=20 exported from these C stubs, hence the export "C". -2- be linked with C++ code. Hence the C++ code within the Extern "C"=20 brackets. > Another question: I could also try to do the vice-versa, and use Ocaml = as > libraries from C++. Has anybody tried doing this? Is it easy to do? I would stick with embedding C++ code into OCaml than the reverse. It's=20 likely that you'd get more answers this way. > Luca --=20 Guillaume Yziquel http://yziquel.homelinux.org/