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
How to call OCaml from C++
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2010-02-24 (01:11)
From: Michael Ekstrand <michael@e...>
Subject: Re: How to call OCaml from C++
On 02/22/2010 09:11 PM, Jianzhou Zhao wrote:
> Hi,
> Does anyone call OCaml code from C++?
> C can call OCaml with the OCaml foreign interfaces.
> But when we call OCaml from C++, we need to consider ‘extern C'
> stuff, like what we do when call C from C++, because the bridge
> functions between C and OCaml are written in C.
> However, those functions have not been prefixed with 'extern C',
> and some functions, such as caml_alloc_dummy in libasmrun, have not
> been exposed
> in a head file. So it is hard to define a wrapper to let it work in C++.
> When linking C++ and libasmrun, caml_alloc_dummy cannot
> be found by the linker.

If you include your OCaml includes in an 'extern C' block like this:

extern "C" {
#include <caml/mlvalues.h>
/* other OCaml includes */

then the C++ compiler should have the right symbol names so that it can
look things up in the runtime.  The headers do not have extern C
declarations in them, but they are able to be imported cleanly in an
extern "C" block.

- Michael