ocamlocamlfind

Required module 'Yojson' is unavailable without location


I am trying to build a simple program using Yojson: the main.ml file is just

let () = exit 1;

and an mod.ml file contains

open Yojson
open Yojson.Basic.Util
let rec json_to_tree json = 
        let _ = json |> member "key" in
        ()

I have a Makefile which first compiles .ml files, then links. For simplicity, let assume we run the three following commands:

ocamlfind ocamlc -package yojson -g  -c mod.ml
ocamlfind ocamlc -package yojson -g  -c main.ml
ocamlfind ocamlc -package yojson -g  -o inter  mod.cmo main.cmo

The problem is that the linking part produces the following error:

File "_none_", line 1:
Error: Required module `Yojson' is unavailable

which I have no idea to solve. Using the Unix module can lead to similar error which are resolved adding unix.cma when linking, but this doesn't help for Yojson.

For the sake of completeness, I installed yojson via opam, and ocamlfind list | grep yojson returns

yojson              (version: 1.3.3)
yojson.biniou       (version: 1.3.3)

Solution

  • This works.

    ocamlfind ocamlc -package yojson -linkpkg -g -o inter mod.cmo main.cmo
    

    While this works, https://github.com/ocaml/dune is the better option to building ocaml code.