caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jan Kybic <jkybic@gmail.com>
To: Aaron Bohannon <bohannon@cis.upenn.edu>
Cc: OCaml List <caml-list@yquem.inria.fr>
Subject: Re: [Caml-list] linking errors involving cpp files
Date: Tue, 15 Dec 2009 15:50:05 +0100	[thread overview]
Message-ID: <7a9cb3950912150650k5ef8d6a6uf125d035876cdc8d@mail.gmail.com> (raw)
In-Reply-To: <c413fcb70912150550q6d103c39s3431850964ddda39@mail.gmail.com>

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

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


  parent reply	other threads:[~2009-12-15 14:50 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-12-15  5:09 Aaron Bohannon
2009-12-15  6:59 ` [Caml-list] " Robert Roessler
2009-12-15 13:50   ` Aaron Bohannon
2009-12-15 13:59     ` Mark Shinwell
2009-12-15 20:47       ` Aaron Bohannon
2009-12-15 14:50     ` Jan Kybic [this message]
2009-12-15 15:04       ` Basile STARYNKEVITCH

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7a9cb3950912150650k5ef8d6a6uf125d035876cdc8d@mail.gmail.com \
    --to=jkybic@gmail.com \
    --cc=bohannon@cis.upenn.edu \
    --cc=caml-list@yquem.inria.fr \
    --cc=kybic@fel.cvut.cz \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).