I’m trying to solve an algorithm puzzle in Haskell, and to do so I need quite a big data structure. However the problem solving site I submit my solution to, doesn’t use any run time options to allow bigger stack, but I’ve heard that I can use compiler options as pragma. I’ve tried using following pragma in my code:
{-# OPTIONS_GHC -O2 -rtsopts -with-rtsopts=-K32m #-}
Then I compile with ghc --make algo.hs. However, when I run on my machine on some big tests, program crashes with stack overflow, and reports that current stack size is 8MB. On the other hand, when I compile like that:
ghc -rtsopts -with-rtsopts=-K32M --make algo.hs -fforce-recomp
The program works just fine on the same data, without adding any +RTS arguments. I use GHC 7.0.2, but the problem solving site is using 6.12.3, so preferably I’m looking for solution that could work with that old version too.
Remember that the compilation of almost any kind of native binary consists of at least two steps: Actual object compilation (
.hs->.o), and linking (.o,.a,.lib-> executable/.exe/.so/.dlletc)When you compile with this:
… what’s actually happening behind the scenes is basically:
I.e. the
--makeoption tells GHC to compile object files automatically before linking the result, and it fills in a ton of blanks for you. Take note of where the individual command-line flags end up.When you specify that pragma at the top of the file, this is instead what happens (with
ghc --make algo.hs):The
OPTIONS_GHCpragma tells the compiler about options to add when compiling that specific module into an object file. Because-rtsoptsis a linker option (it tells GHC to link in a different set of command-line handling stuff), you can’t specify it when compiling an object file. You must specify it when linking, and such options cannot be specified in a module header.There are two solutions:
.cabalfile what GHC options you want