From: Pascal Brisset <pascal.brisset@cnet.francetelecom.fr>
Date: Fri, 30 Oct 1998 11:17:33 +0100 (MET)
To: Thierry Bravier <thierry.bravier@dassault-aviation.fr>
Subject: Re: problem with ocamlmktop -output-obj
In-Reply-To: <36349D52.1E7B@dassault-aviation.fr>
<19981015192243.14795@pauillac.inria.fr>
Thierry Bravier writes:
> > Are there any clues to help me build this toplevel with C++ code
> > without -output-obj ?
I've been calling C++ libraries from caml code since ocaml-1.03
without resorting to any particular hack (see example below). I tend
to avoid things like global objects and c++ exceptions (which were
seriously flawed at that time), so this might not cover all of your
problems. Also, it may fail with compilers other than gcc (because gcc
called as a linker resolves c++ symbols).
- Pascal Brisset <pascal.brisset@cnet.francetelecom.fr> +33296051928 -
- France Telecom CNET DTL/MSV | 2 av Pierre Marzin | F-22307 Lannion -
-------------------------- mlcell.C ---------------------------------
#include <stdio.h>
extern "C" {
# include <caml/mlvalues.h>
# include <caml/alloc.h>
}
class Cell {
public:
Cell(int init) : val(init) { }
void set(int x) { val = x; }
int get() { return val; }
private:
int val;
};
typedef struct {
final_fun f;
Cell *c;
} mlcell;
static void free_cell(value mlc) {
printf("freeing %p\n", mlc);
delete ((mlcell*)mlc)->c;
}
extern "C" value caml_cell_create(value mlv) {
mlcell *res = (mlcell*)alloc_final(sizeof(mlcell)/sizeof(value),
free_cell, 1, 1000); /* ? */
res->c = new Cell(Int_val(mlv));
return (value)res;
}
extern "C" value caml_cell_set(value mlc, value mlv) {
((mlcell*)mlc)->c->set(Int_val(mlv));
return Val_unit;
}
extern "C" value caml_cell_get(value mlc) {
int v = ((mlcell*)mlc)->c->get();
return Val_int(v);
}
-------------------------- cell.ml --------------------------------
module Cell = struct
type t
external create : int -> t = "caml_cell_create"
external set : t -> int -> unit = "caml_cell_set"
external get : t -> int = "caml_cell_get"
end
let _ =
let c = Cell.create 99 in
Printf.printf "%d\n" (Cell.get c);
Cell.set c 42;
Printf.printf "%d\n" (Cell.get c);
flush stdout
let _ = Gc.full_major ()
-------------------------- Testing --------------------------------
$ g++ --version
2.8.1
$ g++ -I/usr/local/lib/ocaml -c mlcell.C
$ ocamlc -v -custom mlcell.o cell.ml -o cell.out
The Objective Caml compiler, version 1.07
Standard library directory: /usr/local/lib/ocaml
$ ./cell.out
99
42
freeing a2944
------------------------------------------------------------------
This archive was generated by hypermail 2b29 : Sun Jan 02 2000 - 11:58:16 MET