caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] interfacing C with Ocaml (beginner questions)
@ 2001-06-07 14:38 francois bereux
  2001-06-07 15:05 ` Markus Mottl
  0 siblings, 1 reply; 3+ messages in thread
From: francois bereux @ 2001-06-07 14:38 UTC (permalink / raw)
  To: caml-list

Dear list members,

I have two (beginnner) questions about linking C and Ocaml.

Following the documentation, I built a very simple example (see code
below) that runs fine by doing :
ocamlc -c add.c
ocamlc -c math.mli
ocamlc -c prog.ml
ocamlc -custom -o myprog myprog.cmo add.o

If I run ./myprog, I get the expected result.

a) Is it possible (and if yes, how shall I do) to build a toplevel where
my add function defined in C would be available ?

b) I managed to have C and Ocaml communicate using the Val_int and
Int_val macros to convert value to the appropriate int type. But how
should I proceed if I want that add operates on float (in Caml) i.e. on
double in C ?
I found a Double_val macro, but no Val_double. What is the solution ?

Thank you in advance for any help,

François


------------- add.c -----------------
#include  <caml/mlvalues.h>
#include  <caml/memory.h>

value add(value x, value y)
{
   CAMLparam2(x,y);
   return Val_int(Int_val(x)+Int_val(y));
}

------------- math.mli -----------------
external add: int -> int -> int = "add";;

--------------myprog.ml---------------
open Math;;

let x=3 and y=86 in
   print_int (add x y);;



-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 3+ messages in thread
* Re:  [Caml-list] interfacing C with Ocaml (beginner questions)
@ 2001-06-08 17:09 Damien Doligez
  0 siblings, 0 replies; 3+ messages in thread
From: Damien Doligez @ 2001-06-08 17:09 UTC (permalink / raw)
  To: caml-list

>From: francois bereux <francois.bereux@fr.thalesgroup.com>

>I found a Double_val macro, but no Val_double. What is the solution ?

You must use copy_double (which will do the allocation) because
floating-point values are boxed in O'Caml.


>value add(value x, value y)
>{
>   CAMLparam2(x,y);
>   return Val_int(Int_val(x)+Int_val(y));
>}

This is wrong.  You must use CAMLreturn.

-- Damien
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

end of thread, other threads:[~2001-06-08 17:09 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-06-07 14:38 [Caml-list] interfacing C with Ocaml (beginner questions) francois bereux
2001-06-07 15:05 ` Markus Mottl
2001-06-08 17:09 Damien Doligez

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