How to transform the Yojson Library of Ocaml into a .byte file using js_of_ocaml?

75 views Asked by At

I am trying to transform the Yojson with the source code download online into a byte file using js_of_ocaml.

I try the following commands:

%: ocamlfind ocamlc -package yojson -linkpkg -g -o yojson.cmo  

%: ocamlfind ocamlc -package js_of_ocaml -package js_of_ocaml-ppx -linkpkg -g -o -package yojson yojson.byte  

I am trying to compile into bytecode an OCaml file but it give the following error:

%: ocamlfind ocamlc -package js_of_ocaml -package js_of_ocaml-ppx -linkpkg -o preprocessing.byte preprocessing.ml
File "preprocessing.ml", line 4, characters 5-22:
4 | open Yojson.Basic.Util
         ^^^^^^^^^^^^^^^^^
Error: Unbound module Yojson
1

There are 1 answers

0
ivg On

The OCaml compiler will not use any compilation units that are not specified on the command line. In addition, you have to specify them in a correct order, so that a user of a unit will come after that unit, e.g.,

ocamlfind ocamlc -package js_of_ocaml -package js_of_ocaml-ppx -linkpkg -o preprocessing.byte yojson.cmo preprocessing.ml

Also, your invocation,

ocamlfind ocamlc -package yojson -linkpkg -g -o yojson.cmo  

is incorrect and should issue an error. You need to specify the -c option to generate the object file.

With that said, you don't need to do what you're doing at all, like building yojson manually (you could install from opam). In fact, it is much better to use a high-level tool, like dune to build your js_of_ocaml programs. The dune documentation provides detailed instructions on how to do this