Context: I am using stack to profile a build which uses FFI invocations to C++ code (via inline-C, a template haskell extension).
Problem: When running stack build
, the project builds fine. But when running stack build --profile
, however, I get the following g++
linker error:
$ stack build --profile
Sling-0.1.0.0: configure
Configuring Sling-0.1.0.0...
Sling-0.1.0.0: build
Preprocessing executable 'Sling-exe' for Sling-0.1.0.0...
[1 of 4] Compiling SimpleCompositor ( src/SimpleCompositor.hs, .stack-work/dist/x86_64-linux-ncurses6/Cabal-1.24.0.0/build/Sling-exe/Sling-exe-tmp/SimpleCompositor.p_o )
[2 of 4] Compiling Struct ( src/Struct.hs, .stack-work/dist/x86_64-linux-ncurses6/Cabal-1.24.0.0/build/Sling-exe/Sling-exe-tmp/Struct.p_o )
[3 of 4] Compiling Lib ( src/Lib.hs, .stack-work/dist/x86_64-linux-ncurses6/Cabal-1.24.0.0/build/Sling-exe/Sling-exe-tmp/Lib.p_o )
[4 of 4] Compiling Main ( main/Main.hs, .stack-work/dist/x86_64-linux-ncurses6/Cabal-1.24.0.0/build/Sling-exe/Sling-exe-tmp/Main.p_o )
Linking .stack-work/dist/x86_64-linux-ncurses6/Cabal-1.24.0.0/build/Sling-exe/Sling-exe ...
.stack-work/dist/x86_64-linux-ncurses6/Cabal-1.24.0.0/build/Sling-exe/Sling-exe-tmp/SimpleCompositor.p_o: In function `c99J_info':
/tmp/ghc22572_0/ghc_9.p_o:(.text+0x77e): undefined reference to `inline_c_SimpleCompositor_5_c5df445469e64aa7738f9848cfc9b677b917df47'
.stack-work/dist/x86_64-linux-ncurses6/Cabal-1.24.0.0/build/Sling-exe/Sling-exe-tmp/SimpleCompositor.p_o: In function `r90H_info':
/tmp/ghc22572_0/ghc_9.p_o:(.text+0x8e2): undefined reference to `inline_c_SimpleCompositor_4_4128a18e34f611cc08850d5bf36de9ac2707d1b2'
.stack-work/dist/x86_64-linux-ncurses6/Cabal-1.24.0.0/build/Sling-exe/Sling-exe-tmp/SimpleCompositor.p_o: In function `r90I_info':
/tmp/ghc22572_0/ghc_9.p_o:(.text+0xae2): undefined reference to `inline_c_SimpleCompositor_3_d0fe1ab601f5030423845b84b10160e1cbd9d79a'
.stack-work/dist/x86_64-linux-ncurses6/Cabal-1.24.0.0/build/Sling-exe/Sling-exe-tmp/SimpleCompositor.p_o: In function `r90J_info':
/tmp/ghc22572_0/ghc_9.p_o:(.text+0xdc2): undefined reference to `inline_c_SimpleCompositor_2_c9e46c413b75c4c01bb29f4321b9275f056c0911'
.stack-work/dist/x86_64-linux-ncurses6/Cabal-1.24.0.0/build/Sling-exe/Sling-exe-tmp/SimpleCompositor.p_o: In function `r90K_info':
/tmp/ghc22572_0/ghc_9.p_o:(.text+0xfc2): undefined reference to `inline_c_SimpleCompositor_1_fff40540e3a02ca3c27ae8f1c780462c1cbb82d0'
.stack-work/dist/x86_64-linux-ncurses6/Cabal-1.24.0.0/build/Sling-exe/Sling-exe-tmp/SimpleCompositor.p_o: In function `r90L_info':
/tmp/ghc22572_0/ghc_9.p_o:(.text+0x11c2): undefined reference to `inline_c_SimpleCompositor_0_2e4a0d3a8c4b9baa2fc6947c1d65b62df44e96df'
.stack-work/dist/x86_64-linux-ncurses6/Cabal-1.24.0.0/build/Sling-exe/Sling-exe-tmp/SimpleCompositor.p_o: In function `s91O_info':
/tmp/ghc22572_0/ghc_9.p_o:(.text+0x1438): undefined reference to `inline_c_SimpleCompositor_2_c9e46c413b75c4c01bb29f4321b9275f056c0911'
.stack-work/dist/x86_64-linux-ncurses6/Cabal-1.24.0.0/build/Sling-exe/Sling-exe-tmp/SimpleCompositor.p_o: In function `s91W_info':
/tmp/ghc22572_0/ghc_9.p_o:(.text+0x18f0): undefined reference to `inline_c_SimpleCompositor_4_4128a18e34f611cc08850d5bf36de9ac2707d1b2'
.stack-work/dist/x86_64-linux-ncurses6/Cabal-1.24.0.0/build/Sling-exe/Sling-exe-tmp/SimpleCompositor.p_o: In function `s925_info':
/tmp/ghc22572_0/ghc_9.p_o:(.text+0x1c62): undefined reference to `inline_c_SimpleCompositor_5_c5df445469e64aa7738f9848cfc9b677b917df47'
.stack-work/dist/x86_64-linux-ncurses6/Cabal-1.24.0.0/build/Sling-exe/Sling-exe-tmp/SimpleCompositor.p_o: In function `s92u_info':
/tmp/ghc22572_0/ghc_9.p_o:(.text+0x2363): undefined reference to `inline_c_SimpleCompositor_0_2e4a0d3a8c4b9baa2fc6947c1d65b62df44e96df'
/tmp/ghc22572_0/ghc_9.p_o:(.text+0x2534): undefined reference to `inline_c_SimpleCompositor_1_fff40540e3a02ca3c27ae8f1c780462c1cbb82d0'
collect2: error: ld returned 1 exit status
`g++' failed in phase `Linker'. (Exit code: 1)
-- While building package Sling-0.1.0.0 using:
/home/user/.stack/setup-exe-cache/x86_64-linux-ncurses6/setup-Simple-Cabal-1.24.0.0-ghc-8.0.1 --builddir=.stack-work/dist/x86_64-linux-ncurses6/Cabal-1.24.0.0 build exe:Sling-exe --ghc-options " -ddump-hi -ddump-to-file"
Process exited with code: ExitFailure 1
Weirdly enough: If I delete .stack-work
from my project and then run the following sequence:
$ stack build
$ stack build --profile
The profiling works!
Why is this? Relying on this hack to get profiling working can't be good.
It looks that there are references being generated by Template Haskell that are not available when doing the profile.
As answered in this question,
c-inline
is differentiated frominline-c
by the fact that it always generates the same identifiers basing the naming on the function contents.That's why you could get those linker errors:
stack build --profile
, incremental compilation is triggered and the identifiers are not created again, but the calls do, resulting in a linker error.When you delete your
.stack-work
folder you force stack to rebuild the whole project and generate the identifiers correctly.A solution could be relying on a
Makefile
that cleans the project cache by removing.stack-work
.