简体   繁体   English

OCaml - 错误:模块“Unix”不可用

[英]OCaml - Error: Module `Unix' is unavailable

I have been working through Harvard's CS51 class using materials available online.我一直在使用在线提供的材料研究哈佛的 CS51 class。 I'm trying to start the final project and downloaded the necessary files, but when I try to compile them I get the following error:我正在尝试启动最终项目并下载必要的文件,但是当我尝试编译它们时,出现以下错误:

Error: Module `Unix' is unavailable (required by `Thread')
Command exited with code 2.
Compilation unsuccessful after building 18 targets (15 cached) in 00:00:00.

I have not made any changes to the code I downloaded yet and supposedly I should be able to compile it successfully in its current state.我还没有对我下载的代码进行任何更改,据说我应该能够在其当前的 state 中成功编译它。 Any ideas why I might be getting this error?任何想法为什么我可能会收到此错误?

Just add只需添加

open Unix;;

at the very start of your.ml file在 your.ml 文件的开头

Generally, you have to explicitly ask to be linked to the Unix module.通常,您必须明确要求链接到Unix模块。

The following program:以下程序:

$ cat main.ml
Unix.gethostname () |> print_endline

would need to be built like this:需要像这样构建:

$ ocamlfind opt -linkpkg -package unix -o main main.ml; echo $?
0

whereas the bare minimum would fail with a similar error as yours:而最低限度会因与您的错误类似的错误而失败:

$ ocamlopt -o main main.ml; echo $?
File "main.ml", line 1:
Error: No implementations provided for the following modules:
         Unix referenced from main.cmx
2

That said, it looks like you're using Core , in which case (as well as most other cases, actually) you're probably better off with dune :也就是说,看起来您正在使用Core ,在这种情况下(实际上以及大多数其他情况)您可能最好使用dune

$ cat dune
(executable
   (name main)
   (libraries unix))

$ dune build main.exe

$ ./_build/default/main.exe
amam-oy

However, if you ask Dune to link you to Core , Unix is already included automatically, so the following dune file would also work for the above program:但是,如果您要求 Dune 将您链接到CoreUnix已经自动包含在内,因此以下dune文件也适用于上述程序:

$ cat dune
(executable
   (name main)
   (libraries core))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM