English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
Producing a C wrapper with ocamlbuild or OMake
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2007-03-28 (08:53)
From: Nicolas Pouillard <nicolas.pouillard@g...>
Subject: Re: [Caml-list] Producing a C wrapper with ocamlbuild or OMake
On 3/28/07, Joel Reymont <joelr1@gmail.com> wrote:
> Folks,
> I'm trying to produce a C library from my OCaml code. I found these
> instructions on the net:
> ocamlopt -output-obj -o fibcaml.o fib.ml
> ocamlopt -c fibwrap.c
> cp /usr/local/lib/ocaml/libasmrun.a libfib.a
> ar r libfib.a fibcaml.o fibwrap.o
> How would I accomplish the same with ocamlbuild, which I'm using now,
> or with OMake?

Just add some rules...

Here is a plugin example that I will add to the example directory.

open Ocamlbuild_plugin;;
open Command;;

let cc = A"cc";;
let ar = A"ar";;
let libasmrun = !*Ocamlbuild_pack.Ocaml_utils.stdlib_dir/"libasmrun.a";;

dispatch begin function
| After_rules ->
    rule "output C obj"
      begin fun env _ ->
        let caml_o = env "%caml.o" and ml = env "%.ml" in
        Cmd(S[!Options.ocamlopt; A"-output-obj"; P ml; A"-o"; Px caml_o])
    rule "build C lib"
      ~deps:["%wrap.o"; "%caml.o"]
      begin fun env _ ->
        let wrap_o = env "%wrap.o" and caml_o = env "%caml.o"
        and lib_a = env "lib%.a" in
        Seq[cp libasmrun lib_a;
            Cmd(S[ar; A"r"; Px lib_a; P caml_o; P wrap_o])]
    rule "build main"
      ~deps:["libfib.a"; "main.o"]
      begin fun _ _ ->
        Cmd(S[cc; P"main.o"; P"libfib.a"; A"-o"; Px"main"])
| _ -> ()

Nicolas Pouillard