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-07 14:38 [Caml-list] interfacing C with Ocaml (beginner questions) francois bereux
@ 2001-06-07 15:05 ` Markus Mottl
  0 siblings, 0 replies; 3+ messages in thread
From: Markus Mottl @ 2001-06-07 15:05 UTC (permalink / raw)
  To: francois bereux; +Cc: caml-list

On Thu, 07 Jun 2001, francois bereux wrote:
> 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 ?

Yes, it is possible: just use the command "ocamlmktop" to link your
program, using all required files as arguments. This will produce
a toplevel.

If you find it cumbersome to remember how to link stuff for specific
targets, you can also use "OcamlMakefile", which automates all these
steps. Then you'd only have to write "make top" if you want to create
a toplevel out of your project. You can get it here:

  http://www.ai.univie.ac.at/~markus/home/ocaml_sources.html

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

You have to use "copy_double" if you want to make an OCaml-float out
of a C-double. Note that this really allocates memory, it's not just a
conversion. See the chapter on interfacing to C in the manual for details:

  http://caml.inria.fr/ocaml/htmlman/manual030.html

Regards,
Markus Mottl

-- 
Markus Mottl, mottl@miss.wu-wien.ac.at, http://miss.wu-wien.ac.at/~mottl
-------------------
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

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