caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Pascal Brisset <pascal.brisset@cnet.francetelecom.fr>
To: Thierry Bravier <thierry.bravier@dassault-aviation.fr>
Cc: caml-list@inria.fr
Subject: Re: problem with ocamlmktop -output-obj
Date: Fri, 30 Oct 1998 11:17:33 +0100 (MET)	[thread overview]
Message-ID: <13881.37437.266875.483207@lsun162> (raw)
In-Reply-To: <36349D52.1E7B@dassault-aviation.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

------------------------------------------------------------------




  reply	other threads:[~1998-10-30 16:36 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-10-14 16:47 Thierry Bravier
1998-10-15 17:22 ` Xavier Leroy
1998-10-16 10:40   ` Thierry Bravier
1998-10-26 16:03     ` Thierry Bravier
1998-10-30 10:17       ` Pascal Brisset [this message]
1998-10-30 17:56         ` problem with ocamlmktop (contd) Pascal Brisset
1998-11-03  9:32           ` Pascal Brisset
1998-11-04 17:56           ` Thierry Bravier
1998-11-04 16:12   ` problem with ocamlmktop -output-obj luther

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=13881.37437.266875.483207@lsun162 \
    --to=pascal.brisset@cnet.francetelecom.fr \
    --cc=caml-list@inria.fr \
    --cc=thierry.bravier@dassault-aviation.fr \
    /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).