Can't compile a program with [@@deriving ...]

I try the following expression :

open Base
type t = int*int [@@deriving compare, sexp];;

if I use :

ocamlfind.exe ocamlc -package base  -ppx "ppx-base -as-ppx"   a.ml

I have the error :

Unbound module Ppx_sexp_conv_lib

Then I try :

ocamlfind.exe ocamlc -package base,ppx_base  -ppx "ppx-base -as-ppx"   a.ml

Now the Base package seems unavailable :

$ocamlfind.exe ocamlc -package base,ppx_base  -ppx "ppx-base -as-ppx"   a.ml
File "a.ml", line 1:
Error: Required module `Base' is unavailable

I am quite clueless about the way to compile this program.

Note that I have no trouble to use open Base and the type expression in utop.

The recommended way these days is to use dune on WSL

In this particular case adding -linkpkg option should help

ocamlfind.exe ocamlc -package base,ppx_base  -ppx "ppx-base -as-ppx"  -linkpkg a.ml

If ocamlfind works on WSL (or whatever OS you’re using) then you can also use ocamlfind thus (I put your code into foo.ml):

ocamlfind ocamlc -package ppx_compare,ppx_sexp_conv -c foo.ml

While finding the particular opam package to install, in order to have certain findlib packages available (e.g. ppx_compare, ppx_sexp_conv) isn’t trivial, after that, well, using ocamlfind makes invoking PPX derivers and rewriters pretty straightforward.

The recommended way these days is to use dune on WSL

Who does recommend this? (Serious question).

The problem I have with dune (as a relatively new OCaml user) is that it
needs a configuration file - thus requiring me to learn yet another
configuration language.

Whereas ocamlbuild can be invoked with arbitrary arguments ad-hoc.

Personally I’ve found the time learning the basics of dune to be worth it

I found this tutorial to be one of the better introductions to OCaml: https://shonfeder.gitlab.io/ocaml_webapp/

particularly the 3rd point since it shows how to use dune, libraries, ppx, some conventional way of using make files, opam switches, code formatting and linting. and it does it without assuming previous knowledge. TBH I would have quit trying OCaml if it wasn’t for this tutorial.
also check out https://dune.readthedocs.io/en/stable/usage.html

2 Likes

Yes, the -linkpkg option works. Thanks a lot. I will look at the dune option, but it is perhaps overkill for a little example made to learn one or two things.

[begin joke]
Why, Jane Street, of course! It’s all part of developer marketing!
[end joke]

But seriously, there is never any need to use dune, and I’ve built quite complex projects with many, many interesting dependencies on both OCaml and C/C++ code, lots of PPX rewriters, and all using Makefiles. it’s not hard, and frankly, I don’t understand why people want all these other tools. But hey, de gustibus and all that.

The OCaml Platform maintainers recommend it. See OCaml Platform for details.

Yeah, it can be a bit of a pain, but it doesn’t take much to get started–see https://dune.build/ , and it pays for itself over and over again with fast incremental builds, as many times as you run the builds.

This is only true for the simplests of builds; you will need to add a _tags file and/or a myocamlbuild.ml plugin as your project grows… For such a simple project, the necessary dune metadata is just 2 or 3 lines.

Other advantages of dune over ocamlbuild are: much faster than ocamlbuild (but you will hardly notice this in a little example), automatic generation of .merlin files, native Windows support, composable (just drop a clone of a dune project inside your repository and it becomes available for use), native support for building “wrapped” libraries using module aliases, etc. And also, it is currently maintained :slight_smile:

Most of these are also advantages with respect to make, to which we could add out-of-tree builds, checksum-based global dependency calculation (which makes highly parallel builds painless), a lot of built-in knowledge about OCaml which needs to be recreated when using make (eg building C/C++ stubs under both Unix and Windows, both as static libraries for native use and shared libraries for toplevel use), an efficient ppx compilation pipeline, etc.

But indeed you will only notice many of this advantages as your project grows; for a small program of a couple of files, any of the tools will suffice easily.

Cheers,
Nicolas

5 Likes