From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: * X-Spam-Status: No, score=1.4 required=5.0 tests=HTML_MESSAGE,SPF_NEUTRAL autolearn=disabled version=3.1.3 Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by yquem.inria.fr (Postfix) with ESMTP id EC9E0BC6B for ; Thu, 10 Jan 2008 00:47:13 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAIbrhEfAXQInh2dsb2JhbACCb40uAQEBCAopkiCGWw X-IronPort-AV: E=Sophos;i="4.24,264,1196636400"; d="scan'208";a="21071433" Received: from concorde.inria.fr ([192.93.2.39]) by mail4-smtp-sop.national.inria.fr with ESMTP; 10 Jan 2008 00:47:16 +0100 Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id m09NlDHf018685 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=OK) for ; Thu, 10 Jan 2008 00:47:13 +0100 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAP7rhEdIDtybgWdsb2JhbACCb40uAQEIBQQpkiGGWw X-IronPort-AV: E=Sophos;i="4.24,264,1196636400"; d="scan'208";a="5887396" Received: from fg-out-1718.google.com ([72.14.220.155]) by mail2-smtp-roc.national.inria.fr with ESMTP; 10 Jan 2008 00:47:13 +0100 Received: by fg-out-1718.google.com with SMTP id 22so420797fge.25 for ; Wed, 09 Jan 2008 15:47:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to:subject:mime-version:content-type; bh=Y2R4If/ncbUgQYzzwX9fsiE1FgjFESi5ESrspHvf494=; b=LefXlWDV/Fge+jFGFsFNCoV673Bl2Jezkki01E/vYCwRTGFBJJC6Bja5fieYebL9mMNjf3nnyqvoPxJBEPZVFV2IQaqDHwGpGi4wWnx082yMRfDTiPyyQieJOKWQJn/7ujRUucgYCQv6AaFMkZZvdJkmKZDQZT5RL98D7e9cxQI= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:mime-version:content-type; b=j5Y8rI/7Sy91fDsdahBFwkuM6JwNrQUCC4eMDUEqpJejEhql5aZCJBLFkvQcaLEJ9y0WRAfZ4NNCJgXtvvJu+dMViRE4O8siYR1T7GfCp30r2LeSq3Id4+pbKsP3bEgSGW80Wp/T3BO0rGAcCjmIsmWroBxgrzAp8BUCUlBnpPI= Received: by 10.78.188.10 with SMTP id l10mr1645638huf.14.1199922432154; Wed, 09 Jan 2008 15:47:12 -0800 (PST) Received: by 10.78.180.12 with HTTP; Wed, 9 Jan 2008 15:47:12 -0800 (PST) Message-ID: Date: Wed, 9 Jan 2008 18:47:12 -0500 From: "Ashish Agarwal" To: caml-list Subject: simple example of using ocamlbuild with C MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_9168_22144430.1199922432146" X-Miltered: at concorde with ID 47855D01.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; wiki:01 stdio:01 stdlib:01 mlvalues:01 camlparam:01 wosize:01 val:01 wosize:01 camlreturn:01 mli:01 val:01 byte:01 byte:01 usr:01 lib:01 ------=_Part_9168_22144430.1199922432146 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline 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 #include #include #include 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 ------=_Part_9168_22144430.1199922432146 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline 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

------=_Part_9168_22144430.1199922432146--