Jim Burton | 21 Dec 01:34
Gravatar

How to use #ifdef WIN32


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?

Thanks,
--

-- 
View this message in context: http://www.nabble.com/How-to-use--ifdef-WIN32-tp14448173p14448173.html
Sent from the Haskell - Glasgow-haskell-users mailing list archive at Nabble.com.
Neil Mitchell | 21 Dec 02:16

Re: How to use #ifdef WIN32

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
(Continue reading)

Jim Burton | 21 Dec 02:30
Gravatar

Re: How to use #ifdef WIN32


Neil Mitchell wrote:
> Hi Jim
>
> <answer>
>
> #if defined(mingw32_HOST_OS) || defined(__MINGW32__)
>
> </answer>

Thanks Neil,

Jim

>
> <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
(Continue reading)

Spencer Janssen | 21 Dec 04:16

Re: How to use #ifdef WIN32

On Thursday 20 December 2007 18:37:15 Jim Burton wrote:
> 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?
>
> Thanks,

If you're using Cabal, something like this should work:

    if os(win32)
        cpp-options: -DWIN32

Cheers,
Spencer Janssen
Duncan Coutts | 21 Dec 05:04

Re: How to use #ifdef WIN32


On Thu, 2007-12-20 at 21:16 -0600, Spencer Janssen wrote:

> If you're using Cabal, something like this should work:
> 
>     if os(win32)
>         cpp-options: -DWIN32

To be precise:

    if os(windows)
        cpp-options: -DWIN32

See, Cabal is (mostly) Neil "I hate mingw" Mitchell compliant. :-)

Duncan
Spencer Janssen | 21 Dec 07:11

Re: How to use #ifdef WIN32

On Thursday 20 December 2007 22:04:13 Duncan Coutts wrote:
> On Thu, 2007-12-20 at 21:16 -0600, Spencer Janssen wrote:
> > If you're using Cabal, something like this should work:
> >
> >     if os(win32)
> >         cpp-options: -DWIN32
>
> To be precise:
>
>     if os(windows)
>         cpp-options: -DWIN32
>
> See, Cabal is (mostly) Neil "I hate mingw" Mitchell compliant. :-)
>
>
> Duncan

Should example 4 in section 2.1.5 of the Cabal manual be changed, then?  Or
are win32 and windows equivalent?

Spencer Janssen
Duncan Coutts | 28 Dec 13:58

Re: How to use #ifdef WIN32


On Fri, 2007-12-21 at 00:11 -0600, Spencer Janssen wrote:
> On Thursday 20 December 2007 22:04:13 Duncan Coutts wrote:
> > On Thu, 2007-12-20 at 21:16 -0600, Spencer Janssen wrote:
> > > If you're using Cabal, something like this should work:
> > >
> > >     if os(win32)
> > >         cpp-options: -DWIN32
> >
> > To be precise:
> >
> >     if os(windows)
> >         cpp-options: -DWIN32
> >
> > See, Cabal is (mostly) Neil "I hate mingw" Mitchell compliant. :-)

> Should example 4 in section 2.1.5 of the Cabal manual be changed, then?  Or
> are win32 and windows equivalent?

Well spotted. Now fixed in darcs.

Duncan

Gmane