You can use stack ghc -- <file names>
to compile and link a set of files (it's briefly mentioned in Stack's user guide):
You'll sometimes want to just compile (or run) a single Haskell source file, instead of creating an entire Cabal package for it. You can use stack exec ghc
or stack exec runghc
for that. As simple helpers, we also provide the stack ghc
and stack runghc
commands, for these common cases.
The --
is to ensure the arguments we pass are sent to ghc
, rather than being parsed as arguments to stack exec
. It's the same thing as when trying to pass arguments to an executable you've made using the normal stack toolchain: stack exec myExe -foo
passes -foo
to exec
, not myExe
, stack exec myExe -- -foo
behaves as desired.
For example:
Bar.hs
module Bar where
bar :: Int
bar = 5
Foo.hs
import Bar
main :: IO ()
main = print bar
Compilation (don't even need to specify Bar.hs
in the build files, it's sourced automatically):
> stack ghc -- Foo.hs
[1 of 2] Compiling Bar ( Bar.hs, Bar.o )
[2 of 2] Compiling Main ( Foo.hs, Foo.o )
Linking Foo ...
> ./Foo
5
There's no problem with dependencies either - it looks like all the packages installed locally are available to the build (you can use containers
or QuickCheck
without any additional build params).