I need to make some little blocks of my program behave differently on different platforms. It seems like GHC is not very good at cross compilation, so I'm planning to compile the same code on Linux and Windows.
What is recommended way to accomplish this? Do I have to write many versions of one module, or there are something like preprocessor directives that I can put into my code to conditionally compile one block of code or another?
P.S. I'm using GHC and Cabal for management.
GHC supports running your code through a C preprocessor. It's not a very Haskelly solution, but it's what's used in practice when necessary.
EDIT: Zeta pointed out there is a per-file language option for this (which I really knew but it's not mentioned in the above link):
{-# LANGUAGE CPP #-}
For Cabal, you can use the extensions
field. To quote, since it mentions this very use case:
extensions: identifier list
A list of Haskell extensions used by every module. Extension names are the constructors of the Extension type. These determine corresponding compiler options. In particular, CPP specifies that Haskell source files are to be preprocessed with a C preprocessor.
Extensions used only by one module may be specified by placing a LANGUAGE pragma in the source file affected, e.g.: {-# LANGUAGE CPP, MultiParamTypeClasses #-}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With