Extra linker flags are given too early in the command line
-optl linker flags to
ghc on the command line places them in the wrong place in the
ld command line:
They are given before all the Haskell libararies, when they should appear after.
Background: Most linkers like
ld.gold, but not the newer LLVM
lld, work in a way where the order of
-l flags given matters; earlier
-lmylib1 flags are supposed to create "holes" for linker symbols that are to be filled with later
lmylib2 flags that "fill the holes" for these symbols.
As discovered in https://github.com/haskell/cabal/pull/5451#issuecomment-518001240, the
-optl flags appear before the
-lHStext-18.104.22.168 -lHSbinary-0.8.6.0 -lHScontainers-0.6.0.1
flags that GHC adds at the very end.
Haskell libraries typically depend on C libraries, so
-lHS* flags will create holes for the C libraries to fill in, but that only works when those libraries'
-l flags are given after the
-lHS* flags; currently they given before, which is wrong.
This means that Cabal's
--ld-options flag and
.cabal file field are pretty ineffective, unless you use the
--ld-option=--start-group hack as shown here that convinces the classical linkers to not be dependent on the order of linker flags given.
All GHC versions up to and including latest (8.6), on at least Linux (most likely all OSs).