Cross-platform development at Haskell

I need to make several small blocks of my program in different ways on different platforms. It seems that GHC is not very good at cross-compiling, so I plan to compile the same code on Linux and Windows.

How to do it? Do I have to write many versions of one module, or is there something like preprocessor directives that I can put in my code to conditionally compile one block of code or another?

PS I use GHC and Cabal for management.

+6
source share
1 answer

GHC supports running your code through the C preprocessor . This is not a very Haskelly solution, but it is what is used in practice when necessary.

EDIT: Zeta pointed out that there is an option for this for each file (which I really knew, but this was not mentioned in the link above):

{-# LANGUAGE CPP #-} 

For Cabal, you can use the extensions field . To quote, since it mentions this very useful case:

extensions: list of identifiers

List of Haskell extensions used by each module. Extension names are constructors of type Extension. They determine the appropriate compiler options. In particular, CPP indicates that the Haskell source files must be pre-processed by the C preprocessor.

Extensions used by only one module can be specified by placing the LANGUAGE pragma in the damaged source file, for example: {- # LANGUAGE CPP, MultiParamTypeClasses # -}

+6
source

Source: https://habr.com/ru/post/977085/


All Articles