Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi-platform development in Haskell

Tags:

haskell

ghc

cabal

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.

like image 638
Mark Karpov Avatar asked Oct 22 '14 15:10

Mark Karpov


1 Answers

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 #-}

like image 53
Ørjan Johansen Avatar answered Oct 21 '22 06:10

Ørjan Johansen