* 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
* Re: [Caml-list] simple example of using ocamlbuild with C
2008-01-09 23:47 simple example of using ocamlbuild with C Ashish Agarwal
@ 2008-01-10 8:05 ` Matthieu Dubuget
2008-01-10 12:35 ` Type checking question Axel Poigné
2008-01-10 16:25 ` [Caml-list] simple example of using ocamlbuild with C Ashish Agarwal
0 siblings, 2 replies; 6+ messages in thread
From: Matthieu Dubuget @ 2008-01-10 8:05 UTC (permalink / raw)
To: Caml Mailing List; +Cc: Ashish Agarwal
---- myocamlbuild.ml ------
open Ocamlopen Ocamlbuild_plugin
open Command
;;
dispatch begin function
| After_rules ->
flag ["link"; "ocaml"; "byte"] (A"-custom");
dep ["link"; "ocaml"; "use_my_math"] ["avg.o"];
| _ -> ()
end
---- _tags ----- (do not forget the endline before end of file)
<run.{native,byte}>: use_my_math
++++++++++++++++
Works for me.
Salutations
P.S.
I had to modify the beginning of
---- avg.c ---
#include <stdio.h >
#include <stdlib.h>
#include <caml/mlvalues.h>
#include <caml/memory.h>
into
---- avg.c ---
#include <caml/mlvalues.h>
#include <caml/memory.h>
#include <caml/alloc.h>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Type checking question
2008-01-10 8:05 ` [Caml-list] " Matthieu Dubuget
@ 2008-01-10 12:35 ` Axel Poigné
2008-01-10 12:56 ` [Caml-list] " Jacques GARRIGUE
2008-01-10 16:25 ` [Caml-list] simple example of using ocamlbuild with C Ashish Agarwal
1 sibling, 1 reply; 6+ messages in thread
From: Axel Poigné @ 2008-01-10 12:35 UTC (permalink / raw)
To: Caml Mailing List
Hello
why does this work
#class xxx (x:int) =
object(self)
val mutable _x = x
method get = _x
end;;
class xxx : int -> object val mutable _x : int method
get : int end
#class yyy (x:int) =
object(self)
inherit xxx x
method set y = _x <- y
end;;
class yyy :
int ->
object val mutable _x : int method get : int method set : int ->
unit end
#class zzz (x:xxx) =
object(self)
method get = x#get
end;;
class zzz : xxx -> object method get : int endlet yy =
new yyy 8;;
#let zz = new zzz((yy:yyy):>xxx);;
val zz : zzz = <obj>
#zz#get;;
- : int = 8
I would expect that yy is of not type yyy = int -> ...
Axel
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] Type checking question
2008-01-10 12:35 ` Type checking question Axel Poigné
@ 2008-01-10 12:56 ` Jacques GARRIGUE
2008-01-10 15:35 ` Axel Poigné
0 siblings, 1 reply; 6+ messages in thread
From: Jacques GARRIGUE @ 2008-01-10 12:56 UTC (permalink / raw)
To: axel.poigne; +Cc: caml-list
From: Axel Poigné <axel.poigne@iais.fraunhofer.de>
> why does this work
[...]
> #class yyy (x:int) =
> object(self)
> inherit xxx x
> method set y = _x <- y
> end;;
> class yyy : int ->
> object val mutable _x : int method get : int method set : int -> unit end
> #let yy = new yyy 8;;
> val yy : yyy
>
> I would expect that yy is of not type yyy = int -> ...
The confusion is due to the fact a class definition actually defines 4 things:
* the class itself, whose type is displayed
* a class type with same name, which is the displayed type _without_ the
parameters (here, without int ->)
* an object type with same name, which is constructed from the public
methods of the class type
* an extensible object type #yyy, which is unifiable with any subclass of
yyy.
The yyy in "new yyy" is the class, but the one in "val yy : yyy" is
the object type.
Jacques Garrigue
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] Type checking question
2008-01-10 12:56 ` [Caml-list] " Jacques GARRIGUE
@ 2008-01-10 15:35 ` Axel Poigné
0 siblings, 0 replies; 6+ messages in thread
From: Axel Poigné @ 2008-01-10 15:35 UTC (permalink / raw)
To: Jacques GARRIGUE; +Cc: caml-list
Hello Jacques
many thanks for the clarification. The example is an abstraction of a
bigger piece of software with some unexpected behaviour (actually
defining the ttk widget in terms of classes and building new composite
widgets ).
Your answer proves that this is not due to the afore-mentioned problems.
Axel
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] simple example of using ocamlbuild with C
2008-01-10 8:05 ` [Caml-list] " Matthieu Dubuget
2008-01-10 12:35 ` Type checking question Axel Poigné
@ 2008-01-10 16:25 ` Ashish Agarwal
1 sibling, 0 replies; 6+ messages in thread
From: Ashish Agarwal @ 2008-01-10 16:25 UTC (permalink / raw)
To: Caml Mailing List
[-- Attachment #1: Type: text/plain, Size: 774 bytes --]
Thanks!
On Jan 10, 2008 3:05 AM, Matthieu Dubuget <matthieu.dubuget@gmail.com>
wrote:
> ---- myocamlbuild.ml ------
>
> open Ocamlopen Ocamlbuild_plugin
> open Command
> ;;
> dispatch begin function
> | After_rules ->
> flag ["link"; "ocaml"; "byte"] (A"-custom");
> dep ["link"; "ocaml"; "use_my_math"] ["avg.o"];
> | _ -> ()
> end
>
> ---- _tags ----- (do not forget the endline before end of file)
> <run.{native,byte}>: use_my_math
>
> ++++++++++++++++
>
> Works for me.
>
> Salutations
>
> P.S.
>
> I had to modify the beginning of
>
>
> ---- avg.c ---
> #include <stdio.h >
> #include <stdlib.h>
> #include <caml/mlvalues.h>
> #include <caml/memory.h>
>
> into
>
> ---- avg.c ---
>
> #include <caml/mlvalues.h>
> #include <caml/memory.h>
> #include <caml/alloc.h>
>
[-- Attachment #2: Type: text/html, Size: 1286 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