Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
* simple example of using ocamlbuild with C
@ 2008-01-09 23:47 Ashish Agarwal
  2008-01-10  8:05 ` [Caml-list] " Matthieu Dubuget
  0 siblings, 1 reply; 6+ messages in thread
From: Ashish Agarwal @ 2008-01-09 23:47 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 1220 bytes --]

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

[-- Attachment #2: Type: text/html, Size: 1680 bytes --]

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

end of thread, other threads:[~2008-01-10 16:25 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-01-09 23:47 simple example of using ocamlbuild with C Ashish Agarwal
2008-01-10  8:05 ` [Caml-list] " Matthieu Dubuget
2008-01-10 12:35   ` Type checking question Axel Poigné
2008-01-10 12:56     ` [Caml-list] " Jacques GARRIGUE
2008-01-10 15:35       ` Axel Poigné
2008-01-10 16:25   ` [Caml-list] simple example of using ocamlbuild with C Ashish Agarwal

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox