0

When loading OCaml, I receive this message when loading ocaml at the terminal:

   ocaml
    OCaml version 4.07.1

   Unknown directive `require'.

What is the problem exactly?


I had previously modified my ocamlinit file, because I had some problems. It now contains:

(* ## added by OPAM user-setup for ocamltop / base ## 3ec62baf6f9c219ae06d9814069da862 ## you can edit, but keep this line *)


(* Added by OPAM. *)                                                            

let () = try Topdirs.dir_directory (Sys.getenv "OCAML_TOPLEVEL_PATH")
 with Not_found -> ()
;;

#require "yojson";;
#use "topfind";;
#camlp4o
#thread;;
Topfind.don't_load ["compiler-libs.toplevel"];;
#require "core.top";;
#require "core.syntax";;
(* ## end of OPAM user-setup addition for ocamltop / base ## keep this line *)

Edit:

I looked at this question before ocaml command line cannot find “topfind”, however I did not find it helpful, since nowhere in the answers was it specified that you need to run eval $(opam config env) every time before opening ocaml, as someone below has informed me. So I think this person's clarification is useful to have on this site for other people.

user65526
  • 685
  • 6
  • 19
  • How was it installed? Did you modify anything recently? Any configuration changed? – ShadowMitia Feb 13 '19 at 22:16
  • @ShadowMitia I modified the file `~/.ocamlinit`, whose contents I have now included below my question. I can't remember how I installed Ocaml. – user65526 Feb 13 '19 at 22:33
  • `#require` is provided by `topfind`. The error occurs because you try to `#require` before `#use "topfind"`. – glennsl Feb 14 '19 at 04:51
  • @glennsi I put `#use "topfind"` above `#require`, and it makes the problem worse. I now get: `OCaml version 4.07.1` `Cannot find file topfind.` `Unknown directive` `'require'. ` – user65526 Feb 14 '19 at 07:14
  • 1
    How is that worse? Does fewer things work? All I see is that you have more information to help you troubleshoot, which seems like an improvement. Perhaps you need to install `ocamlfind`, or just run `eval $(opam config env)` if you haven't. – glennsl Feb 14 '19 at 08:22
  • @glennsl If I write `eval $(opam config env)` on the terminal and then open ocaml, it works. If I do not write `eval $(opam config env)` on the terminal and then open ocaml, I receive `Cannot find file topfind. Unknown directive `require'.` How come? – user65526 Feb 14 '19 at 08:37
  • See https://stackoverflow.com/questions/30155960/what-is-the-use-of-eval-opam-config-env. You should do this in your shell's init script. If you use bash I think opam adds it to `.bashrc`, but if you use a different shell you have to add it manually. – glennsl Feb 14 '19 at 08:41
  • Possible duplicate of [ocaml command line cannot not find “topfind”](https://stackoverflow.com/questions/47508389/ocaml-command-line-cannot-not-find-topfind) – glennsl Feb 14 '19 at 08:43
  • @glenssl What are environment variables? – user65526 Feb 14 '19 at 08:43
  • https://en.wikipedia.org/wiki/Environment_variable – glennsl Feb 14 '19 at 08:44

1 Answers1

2

You shall put #use "topfind" before any #require directive. So put #require "yojson";; to the end of the .ocamlinit file (also it is a good idea to add it after the comment).

The #require directive is provided by the ocamlfind tool via the topfind script, which is loaded into the toplevel via the #use directive, which is a standard builtin directive for loading files. The topfind file initializes the ocamlfind system in the toplevel, so that the toplevel now can access to the ocamlfind infrastructure and load libraries installed in the system. If you're using opam to install packages, then do not forget to do eval $(opam config env) (or a shorter version, available in opam 2.x eval $(opam env)) in your terminal, before starting the toplevel. E.g.,

eval $(opam config env)
ocaml

and here is the correct contents of the .ocamlinit file:

(* ## added by OPAM user-setup for ocamltop / base ## 3ec62baf6f9c219ae06d9814069da862 ## you can edit, but keep this line *)


(* Added by OPAM. *)                                                            

let () = try Topdirs.dir_directory (Sys.getenv "OCAML_TOPLEVEL_PATH")
 with Not_found -> ()
;;


#use "topfind";;
#camlp4o
#thread;;
Topfind.don't_load ["compiler-libs.toplevel"];;
#require "core.top";;
#require "core.syntax";;
(* ## end of OPAM user-setup addition for ocamltop / base ## keep this line *)

#require "yojson";;
ivg
  • 34,431
  • 2
  • 35
  • 63