Combination of foreign export and template haskell leads to linker error
https://gist.github.com/457065b6ff777f09d862b2e1378d68ab
It seems that if you use a foreign export then you can easily make GHC issue a linker error if you also use template-haskell in the same unit.
ghc P Main foo.c -this-unit-id p1-0 -package-name p1 -odir=dist -hidir=dist -stubdir=dist
In this example, we define a function foo
, which is foreign export
ed and then used in foo.c
. The main function contains a TH splice which causes the linker to get invoked and leads to this error:
GHC.Linker.Loader.dynLoadObjs: Loading temp shared object failed
During interactive linking, GHCi couldn't find the following symbol:
/run/user/1000/ghc2096310_0/libghc_18.so: undefined symbol: foo
This may be due to you not asking GHCi to load extra object files,
archives or DLLs needed by your current session. Restart GHCi, specifying
the missing library using the -L/path/to/object/dir and -lmissinglibname
flags, or simply by naming the relevant files on the GHCi command line.
Alternatively, this link failure might indicate a bug in GHCi.
If you suspect the latter, please report this as a GHC bug:
https://www.haskell.org/ghc/reportabug
If you look at the linker command, it includes foo.o
, which has an undefined reference to foo
, but no the object where foo
is defined (dist/P.o
).
/nix/store/8pbwywcj6vbswz7xmy2dh716x8blgh8w-gcc-wrapper-9.3.0/bin/cc '-fuse-ld=gold' -Wl,--no-as-needed -o /run/user/1000/ghc2096310_0/libghc_18.so dist/foo.o -shared -Wl,-Bsymbolic -Wl,-h,libghc_18.so -L/nix/store/5qcip2dpaxrlacm0n9gpia4wg9dzlfrl-ghc-9.2.1/lib/ghc-9.2.1/base-4.16.0.0 -Xlinker -rpath -Xlinker /nix/store/5qcip2dpaxrlacm0n9gpia4wg9dzlfrl-ghc-9.2.1/lib/ghc-9.2.1/base-4.16.0.0 -L/nix/store/5qcip2dpaxrlacm0n9gpia4wg9dzlfrl-ghc-9.2.1/lib/ghc-9.2.1/ghc-bignum-1.2 -Xlinker -rpath -Xlinker /nix/store/5qcip2dpaxrlacm0n9gpia4wg9dzlfrl-ghc-9.2.1/lib/ghc-9.2.1/ghc-bignum-1.2 -L/nix/store/fphpbj8jpyibz0l2xspidg6s7zm7xyb5-gmp-6.2.0/lib -Xlinker -rpath -Xlinker /nix/store/fphpbj8jpyibz0l2xspidg6s7zm7xyb5-gmp-6.2.0/lib -L/nix/store/5qcip2dpaxrlacm0n9gpia4wg9dzlfrl-ghc-9.2.1/lib/ghc-9.2.1/ghc-prim-0.8.0 -Xlinker -rpath -Xlinker /nix/store/5qcip2dpaxrlacm0n9gpia4wg9dzlfrl-ghc-9.2.1/lib/ghc-9.2.1/ghc-prim-0.8.0 -L/nix/store/5qcip2dpaxrlacm0n9gpia4wg9dzlfrl-ghc-9.2.1/lib/ghc-9.2.1/rts -Xlinker -rpath -Xlinker /nix/store/5qcip2dpaxrlacm0n9gpia4wg9dzlfrl-ghc-9.2.1/lib/ghc-9.2.1/rts -L/nix/store/s0mblhs5vmjza9dmipn74rwqflxy1fw7-libffi-3.3/lib -Xlinker -rpath -Xlinker /nix/store/s0mblhs5vmjza9dmipn74rwqflxy1fw7-libffi-3.3/lib -lHSbase-4.16.0.0-ghc9.2.1 -lHSghc-bignum-1.2-ghc9.2.1 -lHSghc-prim-0.8.0-ghc9.2.1 -lgmp -lc -lm