Getting ppx_deriving to work with with ocamlbuild

Hi Folks,
I’m new to OCaml, and I am trying to setup ppx_deriving so that I can print out data structures to gain insight into the output that my algorithms create. I have no prior experience with any OCaml build tools.

I have a basic setup with make and ocamlbuild, but I am unable to get ppx deriving to work.
My project is structured as following:

|-- _build/
|    |-- (generated output)
|-- src/
|    `-- main.ml
`-- Makefile
`-- _tags

Makefile
contains the following code:

.PHONY: all clean byte native profile debug test

OCB_FLAGS = -tag bin_annot -I src
OCB = ocamlbuild $(OCB_FLAGS)

all: native byte # profile debug

clean:
	$(OCB) -clean

native:
	$(OCB) main.native

byte:
	$(OCB) main.byte

profile:
	$(OCB) -tag profile main.native

debug:
	$(OCB) -tag debug main.byte

test: native
	./main.native "OCaml" "OCamlBuild" "users"

and _tags contains:

true: package(ppx_deriving.std)

and main.ml contains:

type test = {
    name: string;
} [@@deriving show]
(* I also tried this with show_test and pp, 
   all of which complained about unbound values *)
let _ = print_string(show_test {
    name = "John Doe"
})

the output of make is:

ocamlbuild -tag bin_annot -I src main.native
File "_tags", line 1, characters 10-26:
Warning: the tag "ppx_deriving.std" is not used in any flag or dependency declaration, so it will have no effect; it may be a typo. Otherwise you can use `mark_tag_used` in your myocamlbuild.ml to disable this warning.
+ /home/gcf/.opam/4.02.3/bin/ocamlc.opt -c -I /home/gcf/.opam/4.02.3/lib/biniou -I /home/gcf/.opam/4.02.3/lib/easy-format -I /home/gcf/.opam/4.02.3/lib/yojson -bin-annot -I src -o src/main.cmo src/main.ml
File "src/main.ml", line 6, characters 21-30:
Error: Unbound value show_test
Command exited with code 2.
Compilation unsuccessful after building 2 targets (1 cached) in 00:00:00.
Makefile:13: recipe for target 'native' failed
make: *** [native] Error 10

Is there any requirement to use ocamlbuild instead of dune? The latter is where a lot of effort and know-how is concentrated (and ppx_deriving works out of the box).

I think that you need to pass -use-ocamlfind to ocamlbuild, for example by passing it in OCB_FLAGS.

In addition I’ll second @Leonidas here, if you’re learning OCaml I’d suggest using dune instead.

The configuration (dune file) looks like:

(executable 
 (name main)
 (preprocess
  (pps ppx_deriving.std)))

And it does the following:

% dune exec ./main.exe
{ Main.name = "John Doe" }

(you can replace print_string by print_endline so that it adds a newline at the end and displays well)

2 Likes

Using ocaml find fixed the issue, thanks so much for the help.

Thanks for the advice on using dune, I’ll make the switch.

1 Like