OCaml:未绑定的Module.function值

时间:2016-04-26 09:44:05

标签: ocaml

我是OCaml的新手,我对模块有点困惑。

我试图实现一个非常简单的测试,但我无法编译它......

以下是文件(顺便说一句,我在Linux上):

main.ml

let main () =
  if ((Array.length Sys.argv) > 2 && int_of_string Sys.argv.(1) > 1 && int_of_string Sys.argv.(2) > 1)
  then
    begin
      Printf.printf "Args = %d && %d\n" (int_of_string Sys.argv.(1)) (int_of_string Sys.argv.(2));
      Laby.initLaby (int_of_string Sys.argv.(1)) (int_of_string Sys.argv.(2))
    end
  else
    Printf.printf "Usage : ./test x y n"

let _ = main ()

Laby.ml

let initLaby (x : int) (y : int) =
  let testCell = Cell.initCell 0 1 in
  begin
    Printf.printf "Init Laby with X(%d) / Y(%d)\n" x y;
    Cell.printCell testCell;
  end

Cell.ml

module type CELL =
  sig
    type  t

    val initCell : int -> int -> t
    val printCell : t ->  unit
  end

module Cell : CELL =
  struct
    type t = (int * int)

    let initCell (x : int) (y : int) =
      (x, y)

    let printCell (x, y) =
      Printf.printf "Cell -> X(%d) / Y(%d)\n" x y

  end

Cell.mli

module type CELL =
  sig
    type  t

    val initCell : int -> int -> t
    val printCell : t ->  unit
  end

module Cell : CELL

这是 Makefile

NAME =  test


ML =    Cell.ml  \
    Laby.ml  \
    main.ml


MLI =   Cell.mli


CMI = $(MLI:.mli=.cmi)
CMO = $(ML:.ml=.cmo)
CMX = $(ML:.ml=.cmx)


OCAMLDPE = ocamldep
CAMLFLAGS = -w Aelz -warn-error A
OCAMLC = ocamlc $(CAMLFLAGS)
OCAMLOPT = ocamlopt $(CAMLFLAGS)
OCAMLDOC = ocamldoc -html -d $(ROOT)/doc


all:        .depend $(CMI) $(NAME)

byte:       .depend $(CMI) $(NAME).byte


$(NAME):    $(CMX)
        @$(OCAMLOPT) -o $@ $(CMX)
        @echo "[OK] $(NAME) linked"

$(NAME).byte:   $(CMO)
        @$(OCAMLC) -o $@ $(CMO)
        @echo "[OK] $(NAME).byte linked"

%.cmx:      %.ml
        @$(OCAMLOPT) -c $<
        @echo "[OK] [$<] builded"

%.cmo:      %.ml
        @$(OCAMLC) -c $<
        @echo "[OK] [$<] builded"

%.cmi:      %.mli
        @$(OCAMLC) -c $<
        @echo "[OK] [$<] builded"

documentation:  $(CMI)
        @$(OCAMLDOC) $(MLI)
        @echo "[OK] Documentation"


re:     fclean all


clean:
        @/bin/rm -f *.cm* *.o .depend *~
        @echo "[OK] clean"


fclean:     clean
        @/bin/rm -f $(NAME) $(NAME).byte
        @echo "[OK] fclean"


.depend:
        @/bin/rm -f .depend
        @$(OCAMLDPE) $(MLI) $(ML) > .depend
        @echo "[OK] dependencies"

这是Makefile的输出:

[OK] dependencies
[OK] [Cell.mli] builded
[OK] [Cell.ml] builded
File "Laby.ml", line 3, characters 17-30:
Error: Unbound value Cell.initCell
Makefile:47: recipe for target 'Laby.cmx' failed
make: *** [Laby.cmx] Error 2

我认为这是一个编译错误,因为它似乎没有找到Cell模块,但我无法使它工作...... 我做错了什么,我该如何解决?

1 个答案:

答案 0 :(得分:1)

每个.ml文件都作为自己的模块。你好Cell里面的模块cell.ml是双倍的。您必须将该功能称为Cell.Cell.initCell。或open Cell中的laby.ml。另外,我认为.ml文件名通常是小写的?旁白:为什么make输出错误的英语?