[英]Ocaml can't find Unix module
I (re-)installed Ocaml on OS X using the following steps:我使用以下步骤(重新)在 OS X 上安装了 Ocaml:
> brew uninstall ocaml
> brew uninstall opam
> brew install ocaml
> brew install opam
> opam init
> eval `opam config env`
> opam switch 4.02.1
> opam install batteries core
I then tried to compile this program:然后我尝试编译这个程序:
open Unix
open Printf
let main () =
match fork () with
| 0 -> printf "child\n"
| pid -> printf "parent\n"
let _ = main ()
I compiled using this command:我使用这个命令编译:
ocamlc -o fork fork.ml
But I get an error:但我收到一个错误:
File "fork.ml", line 1:
Error: Error while linking fork.cmo:
Reference to undefined global `Unix'
In fact, I was getting this error before reinstalling;事实上,我在重新安装之前遇到了这个错误; that is why I reinstalled in the first place.
这就是我首先重新安装的原因。 But the problem persists and I am not sure how to fix it.
但问题仍然存在,我不知道如何解决它。
unix
library is not linked by default, so you need to pass some linking flags, to make it work, eg,默认情况下不链接
unix
库,因此您需要传递一些链接标志以使其工作,例如,
ocamlc unix.cma fork.ml -o fork
If you don't want to know anything about cma
, you can use ocamlbuild
, instead of ocamlc:如果您不想了解有关
cma
任何信息,可以使用ocamlbuild
代替 ocamlc:
ocamlbuild -lib unix fork.native
Or even more general或者更一般
ocamlbuild -pkg unix fork.native
The latter (with pkg
option) would be a preferred way, since it will allow you to specify any package installed with opam
.后者(带有
pkg
选项)将是首选方式,因为它允许您指定任何使用opam
安装的opam
。 Eg, if you would ever try to use lwt
, the you can just link with it with例如,如果您曾经尝试使用
lwt
,则可以将其链接到
ocamlbuild -pkg lwt fork.native
对于那些在使用 OCaml 交互式顶层时遇到此错误的人,命令行语法类似于ocamlc
所需的ocamlc
:
ocaml unix.cma
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.