Browse thread
simple example of using ocamlbuild with C
- Ashish Agarwal
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Ashish Agarwal <agarwal1975@g...> |
| Subject: | simple example of using ocamlbuild with C |
Please can someone explain in simple terms how to build C code with
ocamlbuild. The example at the wiki has more going on than I can understand.
Here's a simple test program:
---- avg.c ---
#include <stdio.h>
#include <stdlib.h>
#include <caml/mlvalues.h>
#include <caml/memory.h>
double avg(double *v, int N) {
int i;
double sum = 0.0;
for (i = 0; i < N; ++i) {
sum += v[i];
}
double ans = sum/N;
return ans;
}
value avg_c(value v) {
CAMLparam1(v);
double ans = avg((double *) v, Wosize_val(v)/Double_wosize);
CAMLreturn(caml_copy_double(ans));
}
--- math.mli ---
val avg : float array -> float
--- math.ml ---
external avg : float array -> float = "avg_c"
--- run.ml ---
print_float (Math.avg [|1.0; 1.5|]);;
print_newline()
Command line instructions for producing byte and native code are as follows.
I would like to please know how to get ocamlbuild to do this.
Byte code:
% cc -c -I /usr/local/lib/ocaml/ avg.c
% ocamlc -c math.mli
% ocamlc -c math.ml
% ocamlc -custom -o run.byte avg.o math.cmo run.ml
% run.byte
1.25
Native code:
% cc -c -I /usr/local/lib/ocaml/ avg.c
% ocamlopt -c math.mli
% ocamlopt -c math.ml
% ocamlopt -o run.native avg.o math.cmx run.ml
% run.native
1.25