OCaml corebuild cannot link to Core.Std
Asked Answered
F

1

5

My problem is similar to this, however, in my case .ocamlinit is set.

Here is my ocaml version.

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

Here is my opam version.

mymac:Desktop myusr$ opam --version
2.0.5

Here is my opam switch.

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

Here's my .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";;

Here is the evidence that I already have core installed.

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).

Here is the sum.ml file from Real World OCaml:

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.)

Here's what happens when I try to build it with 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.

Why isn't corebuild linking to the core library? How can I fix this?

Fibro answered 17/9, 2019 at 2:51 Comment(1)
Have you run eval $(opam config env)? What does which ocamlc say?Huzzah
K
7

The build script loads everything correctly. The module that you're trying to load no longer exists. You're trying to use an old version of the Real World OCaml book together with a very new version of OCaml and Core. The Janestreet Core library has changed a lot since those times. You should either switch to a newer book or downgrade to an older version of OCaml and Core library.

Using modern Core

Since the admission of Dune and module aliases, it is no longer needed to have an extra Std submodule, therefore Janestreet removed it (after a two-year-long deprecation). Therefore, now we're writing

open Core

instead of

open Core.Std (* no longer works *)

The same is true with Core_kernel et alas.

Since OCaml and Janesteet have moved since that time a lot, the newer version of RWO was created with updated examples. It is still a work in progress but looks quite close to be ready. So you can switch to it.

Sticking to older versions of OCaml

If you would like to use the first version of Real World OCaml, then you have to choose a version of OCaml and Janesteet's Core library which are known to be compatible. I failed to find any authoritative recommendations on which version it is better to use with the old book. So I would suggest using OCaml 4.02.3. Then you can install core as usual with opam install core (it should install version 113.33.03), and as far as I remember, it should work with the old book. If you or anyone else is having problems with this version please tell me in the comments section, and I will update this recommendation.

Kicker answered 17/9, 2019 at 12:18 Comment(4)
Thank you, this is a huge help to know that there is a new version of the book in development. I see that the new version uses Dune right away. I managed to compile sum.ml now, though I couldn't figure out how to use the program. That doesn't matter though. mymac:Desktop myusr$ ./_build/default/sum.exe 1. Fatal error: exception (Invalid_argument "Float.of_string ") Raised at file "stdlib.ml", line 30, characters 20-45 Called from file "sum.ml", line 8, characters 44-61 Called from file "sum.ml", line 11, characters 23-47Fibro
the program, expects you to type a sequence of floating-point numbers (e.g., 3.14, 2.17, etc) terminated by an empty line (basically just hit Enter twice, when done). You will be then exposed to the sum of those numbers :)Kicker
note, they should be separated by newlines (not on a single line)Kicker
Ugh, thank god. I thought I was losing my mind.Mink

© 2022 - 2024 — McMap. All rights reserved.