4
votes

In ocaml toplevel, I can use "bitstring" package by typing the following commands:

#use "topfind";;
#camlp4o;;
#require "bitstring.syntax";;

let data = 0l;;
let bits = BITSTRING { data : 32 };;

However, if I create an OCaml script, e.g., foo.ml :

#!/usr/bin/env ocaml
#use "topfind";;
#camlp4o;;
#require "bitstring.syntax";;

let data = 0l;;
let bits = BITSTRING { data : 32 };;

And, if I run the OCaml script, I got a syntax error:

$ ./foo.ml
File "./foo.ml", line 8, characters 28-29: Error: Syntax error

What am I missing here? Why does the same code work with an interactive shell, but not with a script file?

1

1 Answers

4
votes

I believe that's because script file is first parsed, and then the directives are executed, hence it cannot handle unknown (yet) syntax.

The easiest solution is to use ocamlscript :

#! /usr/bin/env ocamlscript
Ocaml.packs := [ "bitstring"; "bitstring.syntax" ]
--

let data = 0l;;
let bits = BITSTRING { data : 32 };;