是否可以使用Core编译OCaml代码而无需corebuild?

问题描述 投票:0回答:2

我使用的是 Ubuntu 18.04。我安装了 OCaml 4.05(通过 apt-get)以及 utop 和 Core(通过 opam)。这就是我的内容

~/.ocamlinit
:

(* Added by OPAM. *)
let () =
  try Topdirs.dir_directory (Sys.getenv "OCAML_TOPLEVEL_PATH")
  with Not_found -> ()
;;

#use "topfind";;
#thread;;
#camlp4o;;
#require "core.top";;
#require "core.syntax";;

编译使用 Core 的 OCaml 代码与 corebuild 配合得很好。但它不适用于普通的 ocamlc 或 ocamlopt。他们抱怨:

错误:未绑定模块核心

我发现 corebuild 非常挑剔。它不喜欢同一目录中的任何现有非 OCaml 代码:

SANITIZE: a total of 12 files that should probably not be in your source tree
  has been found. A script shell file "/home/anta40/Codes/_build/sanitize.sh"
  is being created. Check this script and run it to remove unwanted files or
  use other options (such as defining hygiene exceptions or using the
  -no-hygiene option).
IMPORTANT: I cannot work with leftover compiled files.
ERROR: Leftover object files:
  File hellod.o in . has suffix .o
  File helloscheme_.o in . has suffix .o
  File nqueens.o in . has suffix .o
  File helloscheme.o in . has suffix .o
  File HelloHS.o in . has suffix .o
  File helloml.o in . has suffix .o
ERROR: Leftover OCaml compilation files:
  File nqueens.cmo in . has suffix .cmo
  File helloml.cmo in . has suffix .cmo
  File helloml.cmi in . has suffix .cmi
  File nqueens.cmi in . has suffix .cmi
  File nqueens.cmx in . has suffix .cmx
  File helloml.cmx in . has suffix .cmx
Exiting due to hygiene violations.
Compilation unsuccessful after building 0 targets (0 cached) in 00:00:00.

使用

-no-hygiene
选项,例如
corebuild sum.ml -no-hygiene
不会生成任何本机可执行文件。有什么解决方法吗? `

ocaml
2个回答
0
投票

OPAM 包管理器可以支持多个 OCaml 实例,其中每个实例将 OCaml 安装(

ocaml
ocamlc
ocamlopt
等)与已安装的包分组。每个实例称为一个switch

键入

ocaml switch
查看实例列表。就开关而言,使用软件包(apt-get)安装 OCaml 称为 system

最好避免混合系统和 OPAM 安装。 我建议您创建一个新交换机,确保 OPAM 设置来源正确,并将 Core 安装到新交换机中:

opam switch 4.06.1
eval $(opam config env)
opam install core

自动构建工具假设它们控制整个构建过程,并在发现其他编译结果时抱怨。通常,运行建议的 shell 脚本并重试可以回避问题,即使只是为了发现另一个问题。您的第二个问题(

-no-hygeine
)可能与安装问题有关。


0
投票

您的安装已过时。

我想您遵循了现实世界 OCaml 中的说明,但 OCaml 生态系统自 RWO 第一版以来已经发生了变化。要获得现代安装,您应该遵循 Real World OCaml 的开发版本 https://dev.realworldocaml.org/install.html .

回答你的问题,corebuild 是 ocamlbuild 的一个很好的包装器。要使用 corebuild 从

sum.ml
构建可执行文件,您应该使用

corebuild sum.native

对于本机可执行文件或

corebuild sum.byte

用于字节码编译

否则,您可以通过 ocamlfind 调用 ocamlopt:

ocamlfind ocamlopt -package core sum.ml

最后,如果您打算构建更大的项目,您应该看看 dune https://jbuilder.readthedocs.io/en/latest/overview.html .

© www.soinside.com 2019 - 2024. All rights reserved.