43

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 either for windows detection. I am more interested in whether I can use logical operators like that with preprocessor directives, and if yes how, since the above does not work.

Compiling with gcc I get :

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

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Lefteris
  • 3,196
  • 5
  • 31
  • 52

4 Answers4

68

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
  • 119,832
  • 11
  • 95
  • 108
7

You must use #if and special operator defined

cube
  • 3,867
  • 7
  • 32
  • 52
4

I think it should be possible this way:

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

More information here

Fernando Martin
  • 512
  • 1
  • 6
  • 21
1

Use defined:

#if defined(A) || defined(B)
    #include <whatever.h>
#endif
ynimous
  • 4,642
  • 6
  • 27
  • 43