ocaml command line cannot find “topfind”

2019-07-18 22:53发布

问题:

I've installed opam, run opam init, run opam switch 4.06.0 which created a 4.06.0 directory inside ~/.opam, run "eval opam confing env" which exports $OCAML_TOPLEVEL_PATH as ~/.opam/4.06.0/lib/toplevel amongst other things, when launching ocaml I get the dreaded:

$ ocaml
        OCaml version 4.06.0

Cannot find file topfind.
Unknown directive `camlp4o'.
# 

I've looked at this and this neither of which address my issue and I'm at my wits' end (first time setting up OCaml). This is my ~/.ocamlinit:

(* Added by OPAM. *)
let () =
  try Topdirs.dir_directory (Sys.getenv "OCAML_TOPLEVEL_PATH")
  with Not_found -> ()
;;
#use "topfind"
#camlp4o
#thread
#require "core.top"
#require "core.syntax"

EDIT: Looks like I hadn't installed core, installing core resolved that but now amongst the slew of import diagnostics I get:

Exception:
Invalid_argument
 "The ocamltoplevel.cma library from compiler-libs cannot be loaded inside the OCaml toplevel".

And then a bit further down:

Raised at file "pervasives.ml", line 33, characters 25-45
Called from file "toplevel/toploop.ml", line 468, characters 4-128
Called from file "toplevel/topdirs.ml", line 144, characters 10-51
    Camlp4 Parsing version 4.06.0

回答1:

You should run

eval `opam config env`

Note the backticks. They are usually located to the left of the key 1 on most modern keyboards. The command should not output anything, if you see any output it means that you're running it incorrectly. You have to run this command to activate the opam installation every time you start a new shell (unless you've put this command in your shell initialization scripts, like .bashrc)

If the problem persists, then make sure, that you have installed the ocamlfind package,

opam install ocamlfind


回答2:

What seemed to work for me:

  • make sure core is installed (opam install core)
  • make sure camlp4 is installed (opam install camlp4)
  • Insert Topfind.don't_load ["compiler-libs.toplevel"];; in-between #use "topfind";; and #require "core.top";;, as per this. It is an issue that doesn't appear to be fixed in the latest version of core (0.9.2).