• en

Ocamlbuild "Hello, world!" or how to output to the log file

Hello, world!

Put the following in the file myocamlbuild.ml in the root of your project tree:

 (* Ocamlbuild plugin *)
 open Ocamlbuild_pack
 open Ocamlbuild_plugin
 let _ =
   dispatch & function
   | After_options -> Log.dprintf 0 "Hello, world!"
   | _ -> ()

Invoke ocamlbuild. In _build/_log, you can see:

 ### Starting build.
 Hello, world!
 # Compilation successful.

The integer argument to Log.dprinf is the minimum level at which the message will be logged. The ampersand (&) is commonly defined in Ocaml programs by

 let ( & ) f x = f x

and serves as a low-precedence operator for reducing the number of parentheses required.