简体   繁体   中英

OCaml Error: Required module `Core__Core_sys' is unavailable

I'm having trouble linking a very simple OCaml program:

open Core

Format.printf "hello world %s\n"  "foobar";;
Format.printf "argv= %s\n" (Sys.get_argv()).(0) ;;

which I compile with

ocamlfind ocamlc -thread -package core visitor.ml

The compile step always generates the error:

Error: Required module `Core__Core_sys' is unavailable

I've pinned version 4.0.9, and I can see the file:

$ ocamlfind query core
/home/ubuntu/.opam/4.09.0/lib/core

and $ ls -la /home/ubuntu/.opam/4.09.0/lib/core shows

-rw-r--r--   1 ubuntu ubuntu    17891 Dec  3 20:14 core__Core_sys.cmi
-rw-r--r--   1 ubuntu ubuntu    93777 Dec  3 20:14 core__Core_sys.cmt
-rw-r--r--   1 ubuntu ubuntu    75659 Dec  3 20:14 core__Core_sys.cmti
-rw-r--r--   1 ubuntu ubuntu    16958 Dec  3 20:14 core__Core_sys.cmx

I've tried everything I can think of, with no luck. BTW, I notice that the documentation https://ocaml.org/api/Sys.html makes no mention at all of get_argv but if I try just plain Sys.argv I get a warning:

# Sys.argv ;;
Alert deprecated: Core.Sys.argv
[since 2019-08] Use [Sys.get_argv] instead, which has the correct behavior when [caml_sys_modify_argv] is called.

So I conclude that the core OCaml documentation published at ocaml.org is more than two years out of date, How can one obtain up-to-date documentation? ideally documentation that describes these kinds of newbie errors?

You need to link the package by adding the -linkpkg flag:

ocamlfind ocamlc -thread -package core -linkpkg visitor.ml

A few points. First, it looks like you are on a fairly old version of OCaml. Unless you need to stay on 4.09 for some reason, I highly recommend upgrading to the latest version, 4.13.1. Instructions for installing are here: https://ocaml.org/learn/tutorials/up_and_running.html

In case you have any trouble with that, try upgrading to the latest version of opam (the OCaml package manager) and doing opam update to download the most up-to-date package index.

Second, it looks like you are trying to use Jane Street's Core library, which is a third-party package which is intended as a standard library replacement. As such, it has its own version of the OCaml standard library's Sys module, ie Core.Sys . Regarding the alert that you are getting, the Core.Sys.argv value is actually deprecated by Jane Street Core: https://ocaml.janestreet.com/ocaml-core/latest/doc/core/Core__/Core_sys/index.html#val-argv

A single result from get_argv (). This value is indefinitely deprecated. It is kept for compatibility...

This leads us to the final issue, when you try to compile it is unable to find the core package. There are a couple of choices here. First, one option is that the core package and standard library replacement are actually optional; you may not actually need them. If you're an OCaml beginner you could try sticking with the standard library only (so no open Core , no trying to compile with the core package).

Another option, if you decide to keep using the core package, is to the dune build system instead of ocamlfind . Dune is a powerful, modern OCaml build system that handles almost all aspects of package linking during the build, so you don't need to worry about issuing individual compile commands.

Here's what a dune file would look like:

(executable
  (name visitor)
  (libraries core))

And the visitor.ml file would be in the same directory:

let () =
  Printf.printf "hello world %s\n" "foobar";
  Printf.printf "argv= %s\n" Sys.argv.(0)

Then you would run:

dune exec ./visitor.exe

The .exe is a dune convention, executables across operating systems are given this extension.

Finally, in source code you never actually need ;; . More about that here: https://discuss.ocaml.org/t/terminate-a-line-with-or-or-in-or-nothing/8941/21?u=yawaramin

Note on documentation being out of date: it would help if you could point us to where you got the instructions that led you to this point of confusion. There is a lot of effort to clean up install instructions and modernize documentation, but unfortunately there are a lot of outdated getting started guides out there. The 'Up and Running' link I provided at the top of this answer is the best resource.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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