Ocaml:未知指令“要求”

Ocaml: unknown directive `require'

加载 OCaml 时,我在终端加载 ocaml 时收到此消息:

   ocaml
    OCaml version 4.07.1

   Unknown directive `require'.

到底是什么问题?


我之前修改过我的ocamlinit文件,因为我遇到了一些问题。它现在包含:

(* ## 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 *)

编辑:

我在 ocaml command line cannot find “topfind” 之前看过这个问题,但是我觉得它没有帮助,因为答案中没有任何地方指定你需要 运行 eval $(opam config env) 每次之前打开 ocaml,正如下面有人告诉我的那样。所以我认为这个人的澄清对其他人来说很有用。

您应将 #use "topfind" 放在任何 #require 指令之前。所以把 #require "yojson";; 放在 .ocamlinit 文件的末尾(在注释之后添加它也是一个好主意)。

#require指令由ocamlfind工具通过topfind脚本提供,通过#use指令加载到顶层,这是一个标准用于加载文件的内置指令。 topfind 文件在顶层初始化 ocamlfind 系统,这样顶层现在可以访问 ocamlfind 基础设施并加载系统中安装的库。如果您使用 opam 来安装软件包,那么不要忘记在您的终端中执行 eval $(opam config env)(或更短的版本,在 opam 2.x eval $(opam env) 中可用),然后再启动顶层。例如,

eval $(opam config env)
ocaml

这里是 .ocamlinit 文件的正确内容:

(* ## 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";;