Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] interfacing C with Ocaml (beginner questions)
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2001-06-07 (14:40)
From: francois bereux <francois.bereux@f...>
Subject: [Caml-list] interfacing C with Ocaml (beginner questions)
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
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,


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

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

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

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

To unsubscribe, mail  Archives: