I’ve started to read Real World Ocaml. I installed opam, ocaml 5.2, core, and utop. When I start utop and “open Base” it works. When I start ocaml and “open Base”, it says “Unbound module Base”. I sure didn’t expect to see a difference there. I fear that if ocaml can’t ‘open Base’, ocamlc won’t be able to either, and nothing will compile.
There must be a simple answer. Please tell me what it is.
To make the compiler see the dependency you need to declare that you depend on the findlib package base (this goes for all third-party packages, not just base).
How to do that depends on the way you build your project.
That tells me how to make ocamlc ‘open Base’, I suppose, once I find out how to do dune. But what about opening Base in ocaml interactively? Or should I just not do that, and use utop instead?
Note: #require is not a normal directive for ocaml. It is only made available if you use some scripts provided by the findlib library which is part of ocamlfind.
So from a vanilla ocaml prompt, assuming that you have installed the opam package ocamlfind, you can do #use "topfind";; then #require "base";; and after that you can use Base as you wish, for instance through open Base;;.
For ocamlc, the issue will come up again; the solution is a bit different. You can use ocamlfind as your driver for the compilers: ocamlfind ocamlc -package base -c my_file.ml for compile-only commands, ocamlfind ocamlc -package base -linkpkg -o my_exe my_file.cmo to produce an executable (if you replace my_file.cmo with my_file.ml in the last command you can compile and link at the same time).
If I were OP, I’d take this advice to its full extent and follow the Learn OCaml docs in that they correctly label RWO as an intermediate book, not for beginners