English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

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:50)
From: Jan Kybic <jkybic@g...>
Subject: Re: [Caml-list] linking errors involving cpp files
On Tue, Dec 15, 2009 at 2:50 PM, Aaron Bohannon <bohannon@cis.upenn.edu> wrote:
> 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.

What you can do is to create a thin C layer between your Ocaml and C++ code.
I am including a very simple code - an interface to the Cubpack
library in Ocaml.

If your interface is big, you might try some of the authomatic
builders like SWIG (http://www.swig.org) which I understand can wrap
C++ classes for use in Ocaml.


Jan Kybic <kybic@fel.cvut.cz>                       tel. +420 2 2435 5721
http://cmp.felk.cvut.cz/~kybic                      ICQ 20056945

// stub to call Cubpack++ integration routines from Ocaml
// Jan Kybic, February 2004
// $Id: cubpack_stub.cc 322 2004-02-06 18:03:01Z jkybic $

extern "C" {
#include <caml/mlvalues.h>
#include <caml/memory.h>
#include <caml/alloc.h>
#include <caml/callback.h>

#include <cubpack.h>
#include <stdio.h>

// This code is NOT thread safe
// There is nothing to do about it except change Cubpack++

static value ocamlCallback ;

// Call a Ocaml function with two arguments u,v
real evalOcaml(const Point& p) {
  CAMLparam0() ;
  double z ;
  CAMLlocal3(ou,ov,oz) ;
  ou=copy_double(p.X()) ;
  ov=copy_double(p.Y()) ;
  oz=callback2(ocamlCallback,ou,ov) ;
  z=Double_val(oz) ;
  CAMLreturn(z) ;

static double doIntegrate(double a, double r,int m) {
  CAMLparam0() ;
  Point p1(0.0,0.0), p2(0.0,1.0), p3(1.0,0.0);
  TRIANGLE T(p1,p2,p3);
  CAMLreturn (Integrate(evalOcaml,T)) ;

extern "C"
// parameters: absolute and relative error bounds, maximum number
// of evaluations, the function to evaluate
value cubpackIntegrateTriangle (value a,value r,value m,value f) {
  CAMLparam4(a,r,m,f) ; // register for GC
  CAMLlocal1(z) ;
  register_global_root(&ocamlCallback) ;
  ocamlCallback=f ;
  z=copy_double(doIntegrate(Double_val(a),Double_val(r),Int_val(m))) ;
  remove_global_root(&ocamlCallback) ;
  CAMLreturn(z) ;

// end of cubpack_stub.cc