2016-12-05 44 views
0

我在測試文件中使用我的模塊時出現問題。我在這裏創建了一個更簡單的版本,它仍然複製了這個問題。與使用makefile模塊(Ocaml)的問題

(* person.ml *) 
module Person = struct 
    type person = {name: string; age: int} 

    let create_person name age = {name=name; age=age} 

end 

(*makefile *) 
test: 
    ocamlbuild -pkgs oUnit,yojson,str,ANSITerminal test.byte && ./test.byte 

check: 
    bash checkenv.sh 

clean: 
    ocamlbuild -clean 

(* test.ml *) 
open OUnit2 
open Person 

let person = create_person "john" 40 

(* utop *) 
#use "person.ml" 
open Person 
let person = create_person "john" 40 

Output: val person : person = {name = "john"; age = 40} 

(* when I type in "make" in the terminal *) 

ocamlbuild -pkg oUnit test.byte && ./test.byte 
+ /Users/user/.opam/4.03.0/bin/ocamlc.opt -c -I /Users/user/.opam/4.03.0/lib/oUnit -I /Users/user/.opam/4.03.0/lib/ocaml -o test.cmo test.ml 
File "test.ml", line 4, characters 13-26: 
Error: Unbound value create_person 
Command exited with code 2. 
Hint: Recursive traversal of subdirectories was not enabled for this build, 
    as the working directory does not look like an ocamlbuild project (no 
    '_tags' or 'myocamlbuild.ml' file). If you have modules in subdirectories, 
    you should add the option "-r" or create an empty '_tags' file. 

    To enable recursive traversal for some subdirectories only, you can use the 
    following '_tags' file: 

     true: -traverse 
     <dir1> or <dir2>: traverse 

Compilation unsuccessful after building 4 targets (2 cached) in 00:00:00. 
make: *** [test] Error 10 

回答

2

問題來自事實person_create是可訪問的VI Person.create_person不是person_create。在ocaml中,文件已經是一個模塊。因此,當您打開Person時,將打開其名稱爲person.ml的模塊。在該模塊中,您將創建一個名爲Person的模塊,您可以在其中定義person_create。因此,要麼刪除文件中的Person模塊,要麼輸入「Person.create_person」而不是create_person。
當你在utop中測試時:#use指令就像是一個include。所以你失去了文件名,一旦你打開Person就可以直接訪問person_create。