OCaml, ocamlfind and camlp4 quirks

by jasonyeo

I’ve been playing around with OCaml for my final year project. The language is awesome but the ecosystem seems lacking in many ways. Compared to other languages like Ruby, Python and even Haskell, OCaml doesn’t seem to have a lot of people using and talking about it.

The biggest problem I find when using ocaml is the build system. There are a lot of tools to simplify the build process. There’s ocamlbuild and ocamlfind. But I find that the documentations aren’t very helpful. For example, I can’t find any documentations about writing META files for ocamlfind. Neither can I find any guides about writing _tag files for ocamlbuild. I guess the best way around this is for the users of these tools to post their own guides and document their issues. Sp I’m going to write this post so that I can save another user the headaches that I’ve went through.

For my final year project, I’m working with Jeremy Yallop’s deriving library. The library uses camlp4 (original syntax). To install the library to your system, install opam and execute this command:

$> opam install deriving-ocsigen

Opam is a package manager for OCaml libraries. After installing, make sure you have findlib installed. Then test your installation:

$> ocaml
        OCaml version 4.00.1
# #use "topfind";;

You should see a menu with some commands to get you started with findlib. Next execute these commands in your ocaml toploop:

# #camlp4o;;
# #require "deriving-ocsigen.syntax";;

You should see some output showing that the library’s path is added to ocaml’s search path. Next, let’s try to evaluate some code that use the deriving library.

type point = { mutable x : int; mutable y : int }
     deriving (Eq, Show);;

If the above code is successfully evaluated, then you should have the deriving library installed on your system. To compile code that uses the deriving library, you have to use ocamlfind telling it to use the camlp4o (original syntax) and to use the deriving-ocsigen.syntax package.

ocamlfind ocamlc -package deriving-ocsigen.syntax -syntax camlp4o -c point_seq.ml -o point

That’s it. Hope I’ve saved you some headaches when using ocamlfind with libraries that use camlp4. I believe the above commands should work with other libraries that use camlp4 (sexplib) too.