Re: interface to C++

Xavier Leroy (xleroy@pauillac.inria.fr)
Wed, 20 Sep 1995 12:10:00 +0200 (MET DST)

From: Xavier Leroy <xleroy@pauillac.inria.fr>
Message-Id: <199509201010.MAA10850@pauillac.inria.fr>
Subject: Re: interface to C++
To: christ@mdhost.cse.TEK.COM
Date: Wed, 20 Sep 1995 12:10:00 +0200 (MET DST)
In-Reply-To: <9509181753.AA11777@ostar.CSE.TEK.COM> from "christ@mdhost.cse.TEK.COM" at Sep 18, 95 10:53:14 am

> 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,
use
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