Search code examples
ocamlopamocaml-dune

unbound module in OCaml / dune build


I trying out OCaml for the first time and trying to build a couple files together.

When I run:

dune build bin/main.exe

I am getting:

    ocamlc bin/.main.eobjs/main.{cmi,cmo,cmt} (exit 2)
(cd _build/default && /usr/bin/ocamlc.opt -w @a-4-29-40-41-42-44-45-48-58-59-60-40 -strict-sequence -strict-formats -short-paths -keep-locs -g -bin-annot -I bin/.main.eobjs -I lib/.lib.objs -no-alias-deps -opaque -o bin/.main.eobjs/main.cmo -c -impl bin/main.ml)
File "bin/main.ml", line 10, characters 17-25:
Error: Unbound module Rule

Here is my bin/main.ml file:

open Lib

let () =
    let result = Math.add 2 3 in
    print_endline (string_of_int result);
    let result = Math.sub 3 1 in
    print_endline (string_of_int result);
    let result = Zoom.barf 3 1 in
    print_endline (string_of_int result);
    let result = Rule.add 3 1 in
    print_endline (string_of_int result);

in lib/inner/rule.ml, contains:

let add x y = x + y
let sub x y = x - y

so I think I need to import the rule.ml file somehow in the bin/main.ml file?


Solution

  • In Dune, currently the default is that modules in different directories are not visible to each other. This is controlled by the (include_subdirs no) stanza ( https://dune.readthedocs.io/en/latest/dune-files.html#include-subdirs ). With this setting, to make your modules visible to each other, you need to put a dune file with the correct contents in each subdirectory. If you look at how the Dune project itself does it ( https://github.com/ocaml/dune ), your directory structure would look like this:

    myproj/
      bin/
        dune
        main.ml
      lib/
        dune
        inner/
          dune
          rule.ml
    

    The various dune files should contain the correct (library ...) ( https://dune.readthedocs.io/en/latest/dune-files.html#library ) or (executable ...) ( https://dune.readthedocs.io/en/latest/dune-files.html#executable ) stanzas, as needed.

    Edit: after doing the above, you also need to refer to the Rule module by its full 'path', i.e. Lib.Inner.Rule.