[英]Trouble with using modules with 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
问题出在一个事实,即person_create可通过Person.create_person而不是仅person_create访问。 在ocaml中,文件已经是一个模块。 因此,当您打开Person时,您将打开文件名是person.ml的模块。 在该模块中,创建一个名为Person的模块,在其中定义person_create。 因此,您可以删除文件中的“人”模块,或者键入“ Person.create_person”而不是create_person。
在utop中进行测试时:#use指令就像一个include。 因此,您将丢失文件名,并且在打开Person后可以直接访问person_create。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.