OCaml/沙丘构建中的未绑定模块

unbound module in OCaml / dune build

我第一次尝试 OCaml 并试图一起构建几个文件。

当我运行:

dune build bin/main.exe

我得到:

    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

这是我的 bin/main.ml 文件:

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);

在 lib/inner/rule.ml 中,包含:

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

所以我想我需要以某种方式在 bin/main.ml 文件中导入 rule.ml 文件?

在Dune中,目前默认是不同目录下的模块互不可见。这是由 (include_subdirs no) 节 ( 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 ) 控制的,您的目录结构如下所示:

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

各种 dune 文件应根据需要包含正确的 (library ...) ( https://dune.readthedocs.io/en/latest/dune-files.html#library ) or (executable ...) ( https://dune.readthedocs.io/en/latest/dune-files.html#executable ) 节。

编辑:完成上述操作后,您还需要通过完整的 'path' 引用 Rule 模块,即 Lib.Inner.Rule.