我正在将我的应用程序从 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 中运行。
最佳答案
警告 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 拒绝。
解决方法是从 -warn-error
开关中删除 +31
。但一般来说最好的方法是重命名你的模块。由于拥有多个同名模块,人们遇到了许多难以追踪的链接问题,这就是为什么 31 现在默认是一个错误。
附加说明
警告 31 不是编译时警告,而是链接时警告。因此,如果您使用ocamlbuild
,则必须使用-lflags
而不是-cflags
指定-warn-error -a
>.
关于compiler-errors - OCaml 警告 31、编译器库和 ppx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37415476/