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

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Xavier Leroy <xleroy@p...>
Subject: Re: interface to C++
> Please help me answer a question. Is the 'camlc -custom' link
> command able to work with C++ objects?

I can't provide definitive answers, as I'm not an expert in C++. It
seems that the key point is how your C/C++ compiler allows C++
functions to be called from C. You may want to check first the
compiler docs or hotline, or post a query on comp.compilers. I had a
look at the gcc/g++ documentation, and it doesn't say anything about
this, unfortunately.

The Caml Light runtime system is basically a big set of C functions
that is entered at main() and eventually calls the user-provided C
functions. If you can link that with your C++ code so that
initializers get called correctly, then main() is called, the only
remaining problem is to understand how the names of your C++ functions
appear from the C side (I understand most C++ compilers "mangle" names
by adding type information or whatever to the function name.) Then,
just put the mangled names in the Caml "value" declarations, and
everything should work fine. To get your C++ compiler to be called
instead of the standard C compiler for building the custom executable,
        camlc -cc <the C++ compiler> -custom ...

or just hack the camlc command itself, it's a shell script.

It may be needed to define main() in C++ that just calls
Caml's main(), suitably renamed. It will take some more hacking in
camlc and maybe in libcaml.a.

If your compiler has different, incompatible calling conventions for C
and C++, then maybe it provdes some kind of pragma or declaration
to declare an external C++ function in a C file. Then, camlc will have
to edit /tmp/camlprim.$$.c to insert the correct C++ declarations.

Let me know how it works.

- Xavier Leroy