Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
linking errors involving cpp files
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2009-12-15 (14:00)
From: Mark Shinwell <mshinwell@j...>
Subject: Re: [Caml-list] linking errors involving cpp files
On Tue, Dec 15, 2009 at 08:50:33AM -0500, Aaron Bohannon wrote:
> On Tue, Dec 15, 2009 at 1:59 AM, Robert Roessler <> wrote:
> > Aaron Bohannon wrote:
> >>
> >> How do I link C++ code with OCaml?
> > You might try (when mixing C++ and OCaml) wrapping the whole thing in an
> > 'extern "C" {...}' block (*including* the #includes and the entire main
> > function).
> Thanks for the tip.  This does resolve the missing caml symbols (even
> when naming the file with a cpp extension).  However, my real program
> actually uses some C++ features.  I think I could convert it to a real
> C program, but I assumed there would be some other way.

I suspect it will work if only the #include directives pertaining to Caml
header files are marked as 'extern "C"'.  You just need to tell the compiler to
use the C linkage convention rather than the C++ name mangling convention for
the external Caml functions that your program (implicitly or explicitly) calls.
This is done at the function prototype site, not the call site.  Everything
else in the program should be unaffected.

Xavier, could the headers have the guards added?  (I only spot-checked one,
mlvalues.h, and that didn't appear to have it).