'In Ocaml, how can I access the Lib module from the command line?
If I type ocaml
at the command line and then run code involving e.g. the Lib.explode
function, I get the error Error: Unbound module Lib
. How can I fix this?
Solution 1:[1]
You don't give much to go on. I assume Lib
is not the real name, but just an example. You also don't say whether it's a standard OCaml library, a library you have built yourself, or somebody else's library.
Assume it's a library you have built yourself. Then somewhere you have a file named lib.cmo
(for a single module) or lib.cma
(for an archive of multiple modules). To use such a library from inside the OCaml REPL (the so-called "toplevel"):
$ ocaml
# #load "lib.cmo";;
Or:
$ ocaml
# #load "lib.cma";;
Note that you want to type #load
including the #
. The first #
on the line is the prompt from OCaml. This sometimes makes it confusing to read a transcription of an OCaml session.
After loading the library you can refer to names in the library as you usually would, i.e., as Lib.myfun
. You don't refer to them just by the name myfun
(as it is sometimes tempting to assume).
Solution 2:[2]
You may also wish to use ocamlmktop
.
Consider for instance a very basic file test.ml
:
let foo = 42
I compile that with the following and now have test.cmo
.
ocamlc test.ml
Now I'll create a toplevel with that module available.
% ocamlmktop -o mytop test.cmo
% ./mytop
OCaml version 4.13.1
# Test.foo;;
- : int = 42
#
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Jeffrey Scofield |
Solution 2 | Chris |