2

我正在将我的应用程序从 OCaml 4.02.3 移植到 4.03.0。

假设您有以下内容lexer.ml

type t = T [@@deriving sexp]

let () =
  sexp_of_t |> ignore;
  print_endline "hai"

我正在尝试按以下方式运行它:

ocamlbuild -use-ocamlfind -pkg ppx_sexp_conv -cflags '-w @a-4-31' lexer.byte --

但我收到以下错误:

Warning 31: files lexer.cmo and /Users/vladimir/.opam/4.03.0+flambda/lib/ocaml/compiler-libs/ocamlcommon.cma(Lexer) both define a module named Lexer
File "_none_", line 1:
Error: Some fatal warnings were triggered (1 occurrences)

我知道它compiler-libs也有一个名为的模块Lexer,但是它与我的词法分析器发生冲突:

  • 我不是想链接编译器库。我知道它由 使用ppx_sexp_conv,但它是一个预处理器,它不需要将编译器库链接到我的应用程序中。

  • 警告 31 只是一个警告,我明确地试图将其 ( -w @a-4-31) 视为一种解决方法,但这不起作用。它曾经在 4.02.3 中工作。

4

1 回答 1

4

此警告 31 错误是 ocaml 4.03.0 编译器的新默认行为。

当您链接两个同名模块时,OCaml 会为您提供警告 31。这不是 4.03.0 特有的:

$ touch a.ml
$ ocamlc a.ml a.ml
File "a.cmo", line 1:
Warning 31: files a.cmo and a.cmo both define a module named A
File "a.ml", line 1:
Error: Some fatal warnings were triggered (1 occurrences)  <-- This is new in 4.03.0

默认情况下,OCaml 4.02.3 不会将警告 31 作为错误处理,但 OCaml 4.03.0 会:

$ ocamlc -v
The OCaml compiler, version 4.03.0
Standard library directory: /Users/XXX/.opam/4.03.0/lib/ocaml
$ ocamlc -help
...
  -warn-error <list>  Enable or disable error status for warnings according
     to <list>.  See option -w for the syntax of <list>.
     Default setting is "-a+31"

+31导致警告 31 错误。在 OCaml 4.02.3 中,默认设置是"-a". 这就是为什么您的代码不是被 4.02.3 拒绝,而是被 4.03.0 拒绝的原因。

一种解决方法是+31-warn-error交换机中删除。但一般来说最好的方法是重命名您的模块。由于有多个具有相同名称的模块,人们遇到了许多难以追踪的链接问题,这就是为什么 31 现在默认是错误的原因。

附加说明

警告 31 不是编译时警告,而是链接时警告。因此,如果您使用ocamlbuild,则必须指定-warn-error -awith-lflags而不是-cflags

于 2016-05-25T00:40:34.483 回答