GHC:ghc 给出 clang 错误,但 ghc --make 有效

GHC: ghc gives clang error, but ghc --make works

长话短说:我有两个 .hs 文件,其中 Main.hs 使用模块 Lib.hs

当我尝试按照书 Real World Haskell 第 115 页的建议编译它们时,我遇到了问题:

$ ghc -c Lib.hs
$ ghc -o main Main.hs Lib.o 
    [2 of 2] Compiling Main             ( Main.hs, Main.o )
    Linking main ...
    duplicate symbol '_Lib_add1_info' in:
        ./Lib.o
        Lib.o
    duplicate symbol '_Lib_add1_closure' in:
        ./Lib.o
        Lib.o
    ld: 3 duplicate symbols for architecture x86_64
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    `clang' failed in phase `Linker'. (Exit code: 1)

但是,当我简单地使用 ghc --make 时,它工作正常

 $ ghc --make Main.hs
[1 of 2] Compiling Lib              ( Lib.hs, Lib.o )
[2 of 2] Compiling Main             ( Main.hs, Main.o )
Linking Main ...
    $ ls -1
    Lib.hi
    Lib.hs
    Lib.o
    Main
    Main.hi
    Main.hs
    Main.o

谁能解释为什么 ghc --make 有效而 ghc 无效?

顺便说一句,我的ghc版本是

$ ghc --version
The Glorious Glasgow Haskell Compilation System, version 8.10.5

来自GHC docs

--make

In this mode, GHC will build a multi-module Haskell program automatically, figuring out dependencies for itself. If you have a straightforward Haskell program, this is likely to be much easier, and faster, than using make. [..]

This mode is the default if there are any Haskell source files mentioned on the command line, and in this case the --make option can be omitted.

因此你的台词

ghc -o main Main.hs Lib.o 

其实就是

ghc -o main --make Main.hs Lib.o 

将编译 link Main.hs 及其所有依赖项(包括 Lib.o),在 linking 期间添加另一个 Lib.o。这将 link Lib.o 两次,触发 OP 报告的 linker 错误。

我猜这是在编写 Real World Haskell 后在 GHC 中更改的。