OCaml警告31,编译器-libs和ppx

时间:2016-05-24 13:43:11

标签: compiler-errors ocaml warnings ocamlbuild ocamllex

我正在将我的应用程序从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使用,但它是一个预处理器,它不需要将编译器-lib链接到我的应用程序中。

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

1 个答案:

答案 0 :(得分: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 -a而不是-lflags指定-cflags

相关问题