2

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
glennsl
  • 28,186
  • 12
  • 57
  • 75
Nobilis
  • 7,310
  • 1
  • 33
  • 67

2 Answers2

6

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
ivg
  • 34,431
  • 2
  • 35
  • 63
  • But I have (I didn't include the backticks in the post because SO's formatter gets confused if you nest them, I'll update my post since this seems to cause confusion). `ocamlfind` is also installed, I made sure I ran `opam update` and `opam upgrade` – Nobilis Nov 27 '17 at 14:13
  • What does `find ~/.opam -name topfind` say? – ivg Nov 27 '17 at 14:17
  • Looks like I hadn't installed `core` so I installed that, I no longer get that error but now I get `Exception: Invalid_argument "The ocamltoplevel.cma library from compiler-libs cannot be loaded inside the OCaml toplevel".` `find ~/.opam -name topfind ` returns `.opam/4.06.0/lib/toplevel/topfind .opam/4.06.0/lib/ocaml/topfind `. Jut want to mention that I've updated `utop` to the latest version - 2.0.2 – Nobilis Nov 27 '17 at 14:18
  • Apparently, you're trying to follow RWO instructions. The first version of RWO was written for much older versions of OCaml, and the 4.06 was just released and most likely have compatibility issues. I would suggest you to use some older version of OCaml, e.g., `opam switch 4.01.0`. This will allow you to use examples from the book. – ivg Nov 27 '17 at 14:24
  • yes, that's right, probably should have mentioned, is there a more modern alternative that I can look into? Real world OCaml seemed like an obvious place to start – Nobilis Nov 27 '17 at 14:25
  • There is an ongoing work on the next version of the RWO book, with examples, that use modern OCaml (the language evolves quite fast in the past few years). This is a link for the WIP https://dev.realworldocaml.org/ page. But in any case, I would suggest you still to use some older version of the comiler, like 4.04 or 4.05, as 4.06 is too fresh :) – ivg Nov 27 '17 at 14:27
  • Sure, RWO seems to [recommend](https://dev.realworldocaml.org/install.html) using 4.04 so I figured I might as well use the latest, I'll downgrade using `switch` and if that solves it, I'll update the post to reflect that. Appreciate the help. – Nobilis Nov 27 '17 at 14:29
4

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).
Nobilis
  • 7,310
  • 1
  • 33
  • 67