311

I would like to set OR condition in #ifdef directive.?

I tried:

#ifdef LINUX | ANDROID
...
..
#endif 

It did not work? What is the proper way?

2
  • 22
    If you're using | for "or" in your C conditionals too, you're doing it wrong. Commented Sep 25, 2015 at 1:00
  • 5
    Hence the visit to SO to find out how to do it not-wrong. Commented Nov 12, 2021 at 20:59

2 Answers 2

624

Like this

#if defined(LINUX) || defined(ANDROID)
Sign up to request clarification or add additional context in comments.

Comments

80

OR condition in #ifdef

#if defined LINUX || defined ANDROID
// your code here
#endif /* LINUX || ANDROID */

or-

#if defined(LINUX) || defined(ANDROID)
// your code here
#endif /* LINUX || ANDROID */

Both above are the same, which one you use simply depends on your taste.


P.S.: #ifdef is simply the short form of #if defined, however, does not support complex condition.


Further-

  • AND: #if defined LINUX && defined ANDROID
  • XOR: #if defined LINUX ^ defined ANDROID

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.