Where does #load look for files?

On my system #load "nums.cma";; finds /usr/local/lib/ocaml/nums.cma but #load "zarith.cma";; doesn’t find /usr/local/lib/ocaml/site-lib/zarith/zarith.cma.

What is the algorithm that #load "xx" uses to locate the requested file? I couldn’t find the #load documentation in the docs on OCaml - The OCaml Manual It isn’t mentioned under keywords.

$(prefix)/lib/ocaml/site-lib/$(package)/ is supposed to be a place where individual packages are installed. Where is this configured?

(I am only interested in OS-level camel packages, not OPAM at this point.)

There’s a search-path. You can add to it with #directory and there’s a cmdline option for it. I don’t recall how to dump the contents of the current path, but I’ll bet there’s a way.


It mentions 4 steps how #load searches for files. None of them involve environment variables.

OPAM puts files under ~/.opam and sets environment variables. Either this documentation is incomplete, or otherwise how are OPAM modules found under ~/.opam ?

It’s late, and I don’t have that information at my fingertips. If I wanted to find it, I would search the sources of the toplevel (in the ocaml source distribution) for the #directory directive and find the variable that it manipulates. I’m betting it wouldn’t be too hard to find.

I would also look at the findlib sources and see which paths are managed there. And it might give an indication of which variables in the toplevel implementation are relevant, too.

The search path for toplevel directives is described in OCaml - The toplevel system or REPL (ocaml)

  1. In script mode, the directory containing the script currently executing; in interactive mode, the current working directory.
  2. Directories added with the #directory directive.
  3. Directories given on the command line with -I options.
  4. The standard library directory.

I believe that between 3. and 4. the directories listed in the OCAMLTOP_INCLUDE_PATH variable are considered.


1 Like

More precisely:

Additional directories to search for compiled object code files (.cmi, .cmo and .cma). The specified directories are considered from left to right, after the include directories specified on the command line via -I have been searched. Available since OCaml 4.08.

Thank you for this info!

I am working on the FreeBSD OCaml ports. Each package installs into $(prefix)/lib/ocaml/site-lib/$(package)/.

ocaml fails to find .cma files in these per-package directories. This system was set up for a previous version of ocaml. Now with ocaml-4.12 it doesn’t work. Do you know how to make ocaml look into such per-package directories?

I am not familiar with the FreeBSD setup. However, in a standard OCaml environment, I would not expect #load to find package-specific by itself. Finding and loading package-specific cma is generally done with ocamlfind and #require (in the REPL).