OCaml corebuild 不能 link 到 Core.Std

OCaml corebuild cannot link to Core.Std

我的问题类似于 this,但是,在我的情况下设置了 .ocamlinit

这是我的 ocaml 版本。

mymac:Desktop myusr$ ocaml --version
The OCaml toplevel, version 4.08.1

这是我的 opam 版本。

mymac:Desktop myusr$ opam --version
2.0.5

这是我的 opam 开关。

mymac:Desktop myusr$ opam switch
#  switch   compiler                    description
→  4.08.1   ocaml-base-compiler.4.08.1  4.08.1
   default  ocaml-base-compiler.4.08.1  default

这是我的 .ocamlinit:

mymac:Desktop myusr$ cat ~/.ocamlinit
(* ## added by OPAM user-setup for ocamltop / base ## 3ec62baf6f9c219ae06d9814069da862 ## you can edit, but keep this line *)
#use "topfind";;
(* ## end of OPAM user-setup addition for ocamltop / base ## keep this line *)
#thread;;
#require "core.top";;
#require "core.syntax";;

这是我已经安装核心的证据。

mymac:Desktop myusr$ opam install core utop
[NOTE] Package utop is already installed (current version is 2.4.1).
[NOTE] Package core is already installed (current version is v0.12.3).

这是来自 Real World OCamlsum.ml 文件:

open Core.Std

let rev read_and_accumulate accum =
      let line = In_channel.input_line In_channel.stdin in
      match line with
      | None -> accum
      | Some x -> read_and_accumulate (accum +. Float.of_string x)

let () =
  printf "Total: %F\n" (read_and_accumulate 0.)

以下是我尝试使用 corebuild 构建它时发生的情况:

mymac:Desktop myusr$ corebuild sum.native
+ ocamlfind ocamlc -c -w A-4-33-40-41-42-43-34-44 -strict-sequence -g -bin-annot -short-paths -thread -package core -ppx 'ppx-jane -as-ppx' -o sum.cmo sum.ml
File "sum.ml", line 1, characters 5-13:
1 | open Core.Std
         ^^^^^^^^
Error: Unbound module Core.Std
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 2 targets (1 cached) in 00:00:00.

为什么 corebuild 没有链接到核心库?我该如何解决这个问题?

构建脚本正确加载了所有内容。您尝试加载的模块不再存在。您正在尝试将旧版本的 Real World OCaml 书籍与非常新版本的 OCaml 和 Core 一起使用。自那时以来,Janestreet Core 图书馆发生了很大变化。您应该切换到一本较新的书或降级到旧版本的 OCaml 和核心库。

使用现代核心

自从接受 Dune 和模块别名后,不再需要额外的 Std 子模块,因此 Janestreet 将其删除(经过两年的弃用)。因此,现在我们写

open Core

而不是

open Core.Std (* no longer works *)

Core_kernel 等等也是如此

由于 OCaml 和 Janesteet 从那时起已经移动了很多,因此 newer version RWO 是用更新的示例创建的。它仍在进行中,但看起来非常接近准备就绪。所以你可以切换到它。

坚持使用旧版本的 OCaml

如果您想使用 Real World OCaml 的第一个版本,那么您必须选择已知兼容的 OCaml 和 Janesteet 的核心库版本。我没有找到任何权威的建议,说明旧书使用哪个版本更好。所以我建议使用 OCaml 4.02.3。然后你可以像往常一样使用 opam install core 安装核心(它应该安装版本 113.33.03),据我所知,它应该适用于旧书。如果您或其他任何人在使用此版本时遇到问题,请在评论部分告诉我,我会更新此建议。