Version française
Home     About     Download     Resources     Contact us    
Browse thread
How to wrap around C++?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Guillaume Yziquel <guillaume.yziquel@c...>
Subject: Re: [Caml-list] Re: How to wrap around C++?
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/