13

I'm trying to load my modules in .cmo into the toplevel, I had tried:

$ ocaml mymodule.cmo

I got the toplevel prompt, but I couldn't refer to Mymodule

I also tried the

#load "mymodule.cmo"

It did not complain but still can't refer to Mymodule

I also tried

#use "mymodule.ml"

This seems to work, but it does not load the code into the Mymodule namespace, which is a problem because I actually want to load a few modules into top-level, and they refer to each other by their module namespace.

tshepang
  • 12,111
  • 21
  • 91
  • 136
romerun
  • 2,161
  • 3
  • 18
  • 25
  • 1
    Ah, I figured out the cause of my problem is the cmo files are in subdirectories, -- ocaml lib/mymodule.cmo. I had to use -- ocaml -I lib mymodule.cmo to be able to refer to it. – romerun Dec 18 '11 at 19:59
  • 1
    It's great you got things working. You said when you did `#load "mymodule.cmo"` there was no complaint. But the toplevel *will* complain if it can't find the module you specify. Maybe a few things were going wrong at different points. Regards, – Jeffrey Scofield Dec 19 '11 at 01:57

1 Answers1

19

After you do

#load "mymodule.cmo"

you can refer to your module, but you still need to use the module name:

Mymodule.x

If you want to use a simple name (x), you also need to open the module:

open Mymodule

This could be the source of your problem.

Here's a session that shows what I'm talking about:

$ cat mymodule.ml
let x = 14
$ ocaml312
        Objective Caml version 3.12.0
# load "mymodule.cmo";;
# x;;
Characters 0-1:
  x
  ^
Error: Unbound value x
# Mymodule.x;;
- : int = 14
# open Mymodule;;
# x;;
- : int = 14
# 
Jeffrey Scofield
  • 65,646
  • 2
  • 72
  • 108