Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Guillaume Yziquel <guillaume.yziquel@citycable.ch>
To: Luca de Alfaro <luca@dealfaro.org>
Cc: Michael Ekstrand <michael+ocaml@elehack.net>, caml-list@inria.fr
Subject: Re: [Caml-list] Re: How to wrap around C++?
Date: Mon, 08 Feb 2010 18:54:24 +0100	[thread overview]
Message-ID: <4B704FD0.7010104@citycable.ch> (raw)
In-Reply-To: <7b0bd61a1002080803n6cda4bc5g9eeb8d559538598f@mail.gmail.com>

Luca de Alfaro a écrit :
> Thank you very much!  I follow the general lines, but...
> 
>> * Wrap your OCaml includes in 'extern "C" { ... }"
> 
> Here, I am not sure what you mean.  You mean,
> 
> extern "C" {
> #include <caml/mlvalues.h>
> ...
> }
> 
> ?

See the code in my email:

>   /* Including OCaml system. */
>   #define CAML_VALUE value
>   extern "C" {
>     #include <caml/alloc.h>
>     #include <caml/custom.h>
>     #include <caml/mlvalues.h>
>     #include <caml/memory.h>
>     #include <caml/callback.h>
>     #include <caml/fail.h>
>     #include <caml/misc.h>
>   }

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 = (tokenizer *) 0 ;
>   std::string *arg2 = 0 ;
>   CAMLxparam1(ocaml_arg1);
>   CAMLxparam1(ocaml_arg2);
>   std::list< word > result;
>   
>   arg1 = *((tokenizer * *) Data_custom_val(ocaml_arg1)); 
>   {
>     std::string arg2_str(String_val(ocaml_arg2), caml_string_length(ocaml_arg2));
>     arg2 = &arg2_str;
>   }
>   result = (arg1)->tokenize((std::string const &)*arg2);
>   {
>     caml_result = caml_alloc_custom(&custom_swigtype_ocaml_operations, sizeof (void *), 0 ,1);
>     *((void **) Data_custom_val(caml_result)) = 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")
> 
> 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 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++?

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 
could even wrap a .so file generated from C++ code without knowing 
anything about the source code of the .so file. OK, except headers, and 
vendor info and version info of your C++ compiler.

But, you have to compile the C glue as above with extern "C" and the 
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 the
> 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 
exported from these C stubs, hence the export "C".

-2- be linked with C++ code. Hence the C++ code within the Extern "C" 
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 
likely that you'd get more answers this way.

> Luca

-- 
      Guillaume Yziquel
http://yziquel.homelinux.org/


  parent reply	other threads:[~2010-02-08 17:54 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 [this message]
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=4B704FD0.7010104@citycable.ch \
    --to=guillaume.yziquel@citycable.ch \
    --cc=caml-list@inria.fr \
    --cc=luca@dealfaro.org \
    --cc=michael+ocaml@elehack.net \
    /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