在OCaml源代码中使用Unix函数

mat*_*ker 4 unix ocaml

我有一个函数来使用函数time.ml中的unix gettimeofday()来获取当前时间.该文件以下列方式访问该函数:

open Unix; (*declared at the top of the file*)

let get_time ()  = Unix.gettimeofday ()
Run Code Online (Sandbox Code Playgroud)

.mli文件中的相应条目如下所示:

val get_time : unit -> float
Run Code Online (Sandbox Code Playgroud)

但在编译期间会抛出错误:

File "_none_", line 1, characters 0-1:
No implementations provided for the following modules:
Unix referenced from time.cmx
Run Code Online (Sandbox Code Playgroud)

我已检查/ lib/ocaml目录中是否存在以下文件:

unix.cmi, unix.a unix.cma unix.cmx unix.cmxa unix.cmxs unix.mli 
Run Code Online (Sandbox Code Playgroud)

并且路径设置正确设置为.bashrc文件.

LD_LIBRARY_PATH=~/lib
Run Code Online (Sandbox Code Playgroud)

尽管存在于同一个/ lib/ocaml目录中,但来自Printf模块的fprintf等其他函数也能正常工作.

什么想法可能是错的?我做错了什么或者我错过了什么?

cag*_*ago 6

你必须像这样编译:

ocamlc unix.cma -c time.mli time.ml (* bytecode *)
Run Code Online (Sandbox Code Playgroud)

要么

ocamlopt unix.cmxa -c time.mli time.ml  (* native code *)
Run Code Online (Sandbox Code Playgroud)


小智 5

如果您有正确配置的 findlib,

ocamlfind ocamlopt -package Unix -linkpkg time.ml

将为您省去选择 .cma 或 .cmxa 版本的麻烦。