How to use #ifdef WIN32

Neil Mitchell ndmitchell at gmail.com
Thu Dec 20 20:16:40 EST 2007


Hi Jim

> I want to switch code on the OS but this always goes through to the #else (on
> windows or elsewhere):
>
> {-# OPTIONS -cpp #-}
> #ifdef WIN32
> main = putStrLn "hello windows"
> #else
> main = putStrLn "hello something else"
> #endif
>
> Does this depend on a Makefile setting WIN32, or should there be something
> predefined?

<answer>

#if defined(mingw32_HOST_OS) || defined(__MINGW32__)

</answer>

<rant>

If you want to test for Windows you have to ask whether an unrelated
system is installed on your machine. Even if it isn't, you still have
to test this value. This is what I call a "bug".

You might also want to use System.Info.os and test this value
dynamically rather than as a preprocessor, that keeps both code
branches type checking and probably isn't much more expensive at
runtime. Guess what the value of  "os" returns on Windows? (hint: its
ill-typed, being not an operating system)

</rant>

Thanks

Neil


More information about the Glasgow-haskell-users mailing list