caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* How to call OCaml from C++
@ 2010-02-23  3:11 Jianzhou Zhao
  2010-02-24  1:11 ` Michael Ekstrand
  0 siblings, 1 reply; 2+ messages in thread
From: Jianzhou Zhao @ 2010-02-23  3:11 UTC (permalink / raw)
  To: caml-list

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.

Does anyone happen to know how to solve this problem?

Thanks a lot.
Jianzhou


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: How to call OCaml from C++
  2010-02-23  3:11 How to call OCaml from C++ Jianzhou Zhao
@ 2010-02-24  1:11 ` Michael Ekstrand
  0 siblings, 0 replies; 2+ messages in thread
From: Michael Ekstrand @ 2010-02-24  1:11 UTC (permalink / raw)
  To: caml-list

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2010-02-24  1:11 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-02-23  3:11 How to call OCaml from C++ Jianzhou Zhao
2010-02-24  1:11 ` Michael Ekstrand

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).