Search code examples
moduleocaml

Module unavailable when compiling another file that uses it in OCaml


I have multiple compilation units in OCaml, one of which is causing a few problems. One compilation unit has two files foo_one.mli and foo_one.ml, these compile without any errors when using ocamlc. The other compilation unit has two files shoo.mli and shoo.ml. shoo.ml needs to make use of functions from foo_one, and so in the file shoo.ml I use open Foo_one. To compile, I use the command ocamlc foo_one.mli shoo.mli shoo.ml and the compiler returns the error: Module Foo_one is unavailable (required by Shoo).

What is causing this error and how can it be solved?

I made sure that the compilation units are all in the same directory, with the files in one compilation unit having the same name and capitalising the name of the module I am including in shoo.ml.


Solution

  • Assuming you've compiled foo_one.mli and foo_one.ml with:

    ocamlc -c foo_one.mli foo_one.ml
    

    Then you should be compiling shoo.ml with:

    ocamlc foo_one.cmo shoo.mli shoo.ml
    

    If you had provided the -c option, you could compile with the following because you're not yet linking the two, and thus Shoo only needs to know the interface Foo_one exposes.

    ocamlc -c foo_one.mli shoo.mli shoo.ml
    

    If you did this, you could finally link the two together to create an executable with:

    ocamlc foo_one.cmo shoo.cmo