14

I have some c++ code that has a bunch of #ifdef WIN32 else we assume its IOS code. However I am now trying to use this same c++ code for an android port.

Is there some sort of equivalent for #ifdef WIN32 || ANDROID?

Metropolis
  • 6,542
  • 19
  • 56
  • 86

1 Answers1

38

Macro

Regarding predefined macros, there is the famous predef.sf.net.

Looking for Android brings up the devices page. There:

Android

The following macros have to be included from the header file.

Type    | Macro           | Format  | Description
Version | __ANDROID_API__ | V       | V = API Version

Example

Android Version | __ANDROID_API__
1.0             | 1
1.1             | 2
1.5             | 3
1.6             | 4
2.0             | 5
2.0.1           | 6
2.1             | 7
2.2             | 8
2.3             | 9
2.3.3           | 10
3.0             | 11 


Examples

#ifdef __ANDROID__
# include <android/api-level.h>
#endif

#ifdef __ANDROID_API__
this will be contained on android
#endif

#ifndef __ANDROID_API__
this will NOT be contained for android builds
#endif

#if defined(WIN32) || defined(__ANDROID_API__)
this will be contained on android and win32
#endif

If you want to include a code block for versions for a high enough version, you must first check for existence, and then you can do arithmetic comparisons:

#ifdef __ANDROID_API__
# if __ANDROID_API__ > 6 
    at least android 2.0.1
# else 
    less than 2.0.1
# endif
#endif


Multiple conditions

You can't do #ifdef FOO || BAR. The standard only defines the syntax

# ifdef identifier new-line

but you can use the unary operator defined:

#if defined(FOO) && defined(BAR)

you can also negate the result using !:

#if !defined(FOO) && defined(BAR)
   this is included only if there is no FOO, but a BAR.

and of course there's a logical-or:

#if defined(FOO) || defined(BAR)
   this is included if there is FOO or BAR (or both)
Sebastian Mach
  • 38,570
  • 8
  • 95
  • 130
  • Im not really sure how this helps – Metropolis Dec 12 '11 at 14:32
  • @Metropolis: My understanding is that he's looking for a flag. I'll add an example on how to use that. – Sebastian Mach Dec 12 '11 at 14:37
  • This looks promising. Ill try it out and get back to you. Thanks for the extra examples. – Metropolis Dec 12 '11 at 14:53
  • I actually tried "#if defined(WIN32) || defined(__ANDROID_API__)" and it was ignoring that if, but if I changed it to "#if defined(WIN32) || defined(ANDROID)" it works. Any idea why this would be? I took this off of the correct answer until we decide what is right. – Metropolis Dec 14 '11 at 03:30
  • @Metropolis: You must take that macro literally, including underlines: `__ANDROID_API__`. If that doesn't help, then the predef-wiki might not be up to date; your compiler's man-pages/documentation might help (and finally, you might help the predef-wiki ;) ) – Sebastian Mach Dec 15 '11 at 11:15
  • Opps sorry about that. I actually think I did put it in exactly as you have it and it was not working, but let me try it one more time to be positive on that and I will get back to you here. – Metropolis Dec 15 '11 at 16:52
  • Yeah I put it in correctly. If you notice above how the ANDROID_API is in bold, thats because I did not put it in a code block like you did in the comment. – Metropolis Dec 15 '11 at 18:27
  • I see. Then I fear I can't help and you have to take a look into the manual of your SDK / compiler version. – Sebastian Mach Dec 16 '11 at 09:53
  • 1
    Well using ANDROID actually does work, I just do not understand why. – Metropolis Dec 16 '11 at 16:09
  • @Metropolos: As said, the "why" might be "hidden" in the documentation ;) Or maybe have a look at `` (from the predef-site), sometimes headers may contain valuable information. – Sebastian Mach Dec 16 '11 at 16:25