Reputation: 25812
I wish to use module Std
inside my OCaml .ml
file.
I tried #load "Std"
, but the compiler complains.
How can I load a module inside OCaml?
Upvotes: 1
Views: 4021
Reputation: 14730
You must compile the module you wish to include first, provide the location of the compiled files to compilation commands of modules depending on it, then provide it in the final compilation command line.
Let's consider for instance file foo/moduleA.ml
:
let v = 1
and file bar/moduleB.ml
:
open ModuleA
let w = v
The commands:
$ cd foo
$ ocamlc -c moduleA.ml
$ cd ..
will produce moduleA.cmo
and moduleA.cmi
. The former is the bytecode object of the module (like a .o
file in for native object files, but containing bytecode data and text), the later is a bytecode compiled header, produced from an automatically generated .mli
file. This bytecode header is necessary for the compiler to compile files which depend on ModuleA
.
$ cd bar
$ ocamlc -I ../foo -c moduleB.ml
$ cd ..
will succeed in producing moduleB.cmo
, which depends on ModuleA
, because the previous command has been successful, and because we indicate the compiler where to look for dependancies with the -I
command line parameter, followed by the path of the first module.
The last command below will produce a bytecode executable from both modules:
$ ocamlc -I foo -I bar moduleA.cmo moduleB.cmo -o prog.byte
The modules must be provided in that order, to let the compiler know the dependancies first. The -I
parameters this time indicate where to find the .cmo
files.
In your case, you must therefore use the -I <location of std.cmi>
for the compilation proper phase, and -I <location of std.cmo>
(or std.cma
, if it is a library) for the second phase (the link phase). If you can combine both phases in one command (ie. ocamlc -I foo foo/moduleA.ml bar/moduleB.ml -o prog.byte
), and if both cmo
and cmi
files are in the same directory, only one parameter will suffice.
Upvotes: 4