How to #load with a path?

I am using the OCaml REPL and I need to load a precompiled .cmo file. This works fine if the file is in the working directory.

However if the file lies elsewhere, specifying an absolute or relative path does not show an error on loading, but the corresponding module is not available :

# #load "corrige/build/lin/5.0.0/support.cmo";;                                                                                          
# #show Support;;
Unknown element.

I know I can use #cd beforehand, but I find this cumbersome :

# #cd "corrige/build/lin/5.0.0";;
# #load "support.cmo";;
# #show Support;;
module Support :
  sig
    module Patch : sig ... end
    module Types : sig ... end
    module Regexp : sig ... end
    module Determinisation : sig ... end
    module Glushkov : sig ... end
    module Affichage : sig ... end
    module Aide : sig ... end
  end

The toplevel needs to see the cmi file of a module to be able to interact with it. So you should add the directory to the load path. I think this sequence should work for your:

# #directory "corrige/build/lin/5.0.0";;
# #load "support.cmo";;
# #show Support;;

(the reason why it works with the #cd is that . is part of the load path)

2 Likes

Thanks. Can we use variables here ?

I tried to do

#directory ("corrige/build/lin/" ^ Sys.ocaml_version);;

and other variants, but to no avail.

No, toplevel directives (stuff starting with #) work at a “meta” level.

1 Like

If you use the functions from the Topdirs module directly, you should be able to pass arbitrary values:

# Topdirs.dir_directory ("corrige/build/lin/" ^ Sys.ocaml_version);;
# #load "support.cmo";;
# #show Support;;
4 Likes