Where can I find out about #ifdef?

I see that this is often used to make modules compatible with GHC and Hugs, but Google doesn't help me know more about this.

What can I set inside the conditional? Can I make parts of the module conditional, which version of the "base" is used?

EDIT 3/2017 . This is a great resource: https://guide.aelve.com/haskell/cpp-vww0qd72

+34
c-preprocessor haskell ghc
Jun 15 '11 at 17:37
source share
4 answers

The GHC documentation has a section related to the C preprocessor that documents some of the predefined macros of the preprocessor.

The Cabal documentation has a section related to conditional compilation , which gives an example related to base . If you are writing a portable package, you should still use Cabal.

+36
Jun 15 2018-11-17T00:
source share

In addition to the very useful flags defined by the GHC (OS, architecture, etc.), other flags and macros are defined when using bondage.

Check package version

This uses crypto-api , which checks the version of the tagged package being used:

 #if MIN_VERSION_tagged(0,2,0) import Data.Proxy #endif 

Custom CPP Defines Based on Cabal Flags

You can define CPP characters depending on cabal flags. Here's a (optionally complicated) example from pureMD5 (from a .cabal file):

  if arch(i386) || arch(x86_64) cpp-options: -DFastWordExtract 

Inside the .hs module .hs you can use #ifdef , for example:

 #ifdef FastWordExtract getNthWord nb = inlinePerformIO (unsafeUseAsCString b (flip peekElemOff n . castPtr)) #else ... other code ... #endif 

For more information, you can see the Cabal user guide. This page contains conditional compilation information that you are probably looking for.

+23
Jun 15 '11 at 17:50
source share

#ifdef and friends are used by the C preprocessor (CPP). They provide a way to compile code conditionally. You can enable the use of CPP by adding the pragma {-# LANGUAGE CPP #-} on top of the file.

Many Haskell-related programs install some macros for the preprocessor (for example, GHC sets __GLASGOW_HASKELL__ to the GHC version), so you can conditionally compile code, for example, to use various native libraries for Hugs and GHC.

+6
Jun 15 '11 at 17:45
source share

If you run your Haskell compiler with the -cpp option, it first preprocesses the source files using CPP (C Pre Processor).

Take a look at section 4.11.3. Options affecting the C preprocessor are here .

+2
Jun 15 '11 at 17:44
source share



All Articles