views:

735

answers:

5

Hello all, I searched the site but did not find the answer I was looking for so here is a really quick question.

I am trying to do something like that :

#ifdef _WIN32 || _WIN64
     #include <conio.h>
#endif

How can I do such a thing? I know that _WIN32 is defined for both 32 and 64 bit windows so I would be okay with just it for windows detection. I am more interested in wether I can use logical operators like that with preprocessor directives, and if yes how, since the above does not work.

Thanks for any answers people.

EDIT: I forgot to say what's wrong with it. Compiling with gcc I get :

arning: extra tokens at end of #ifdef directive , and it basically just takes the first MACRO and ignores the rest.

+1  A: 

You must use 'if defined' rather than 'ifdef'

#if defined _WIN32 || defined _WIN64

oops typo, you need the second 'defined' that's why your test is failing. Think of it as 'defined _WIN32' being a single statement returning true/false

Martin Beckett
Parens are optional, but the second defined is not. :)
Steve Fallows
Sorry - especialy as that was exactly what was wrong with the OP code!
Martin Beckett
+14  A: 

Try:

#if defined(_WIN32) || defined(_WIN64)
// do stuff
#endif

The defined macro tests whether or not a name is defined and lets you apply logical operators to the result.

Aaron Maenpaa
The parenthesis are optional
Martin Beckett
Thank you , you were right. Exactly what I was looking for. That works fine :)
Lefteris
+1  A: 

I think it should be possible this way:

#if defined block1 || defined block2 /*or any other boolean operator*/
   /*Code*/
#endif

More information here

fmartin
+3  A: 

You must use #if and special operator defined

cube
A: 

Use defined:

#if defined(A) || defined(B)
    #include <whatever.h>
#endif
ynimous