Main Software Downloads Other

ocamlscript: natively-compiled OCaml scripts [difficulty = 1 camel]


ocamlscript compiles scripts, i.e. one-file programs, into natively-compiled binaries, and executes them.

Basically, a script named alpha is compiled into alpha.exe. If alpha.exe is more recent than alpha, then alpha.exe is executed directly, otherwise alpha is recompiled first.

Compilation commands reside in the first section of the script. They are written in OCaml and consist normally in setting one or two variables.


The ocamlscript tool is made of two essential components: a command named ocamlscript and a library which is made available in the compilation section of the scripts.

The basic usage of the ocamlscript command was designed to be intuitive:


Script files do not have to follow the conventions of standard OCaml file names. They can also be read from stdin or directly from the command-line. Full details on the command-line options are returned by ocamlscript --help.

An ocamlscript script differs from a standard OCaml file at its beginning. There is an optional first section that instructs ocamlscript on how to compile the script. Basically, variables that hold package names and additional preprocessing or compilation option can be set. A simple script that fetches a web page using the HTTP client provided by the netclient package could be:

#! /usr/bin/env ocamlscript
Ocaml.packs := [ "netclient" ]
match Sys.argv with
    [| _; url |] -> print_string (Http_client.Convenience.http_get url)
  | _ -> prerr_string "Usage: getpage URL\n"; exit 1

For complete details, see the API of the ocamlscript compilation library.

For simple examples of original uses of ocamlscript, you can browse the collection of examples extracted from the distribution of ocamlscript.

More documentation, although somewhat incomplete, is given on the wiki site.


This software was written by David Mentré and Martin Jambon. It is distributed under a Boost Software License. The current version is 2.0.2; see Changes. Download it here.