Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

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: -- (:)
From: Nicolas Pouillard <nicolas.pouillard@g...>
Subject: Re: [Caml-list] Producing a C wrapper with ocamlbuild or OMake
On 3/28/07, Joel Reymont <> 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
> 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 "" 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