Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where can I learn about #ifdef?

I see this used often to make modules compatible with GHC and Hugs, but google is not helping me learn more about it.

What can I put inside the conditional? Can I make parts of a module conditional on what version of 'base' is in use?

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

like image 442
jberryman Avatar asked Jun 15 '11 17:06

jberryman


People also ask

Where can I learn on the internet?

If you want to take college-caliber courses without the high cost of college tuition, Coursera is the best stop. This website offers amazing classes in all kinds of fields—from professional development and job skills to psychology, history, and literature.


2 Answers

The GHC documentation has a section relating to the C pre-processor that documents some of the predefined pre-processor macros.

The Cabal documentation has a section relating to conditional compilation that gives an example relating to base. If you are writing a portable package, you should be using Cabal, anyway.

like image 149
Lambdageek Avatar answered Sep 22 '22 23:09

Lambdageek


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

Check Package Versions

Here's a use from crypto-api that 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 symbols dependent on cabal flags. Here's an (unnecessarily complex) example from pureMD5 (from the .cabal file):

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

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

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

For more information you can see the Cabal users guide. This page has the "conditional compilation" information you're probably looking for.

like image 41
Thomas M. DuBuisson Avatar answered Sep 18 '22 23:09

Thomas M. DuBuisson