1

When development various platforms(Android,iPhone,Mac,Windows and so on), it is necessary diverged processing(#ifdef/#endif) by depends on an platform definition. But, I don't know original definition in Mac/iPhone/Android.

 Windows : WIN32 (Visual C++)

 Mac : __MAC_NA(?) (XCode)

 iPhone/iPad/iPod : __IPHONE_NA(?) (XCode)

 Android : ?? (AndroidNDK)

By what definition should I divide?

Shiva
  • 596
  • 1
  • 5
  • 12

3 Answers3

4

For iPhone, I believe the define is TARGET_OS_IPHONE and for Android it's ANDROID. I'm not too familiar with Apple-specific stuff, but after poking around I found a great list for tons of OS defines here. There's another answer here on SO that has a method for pulling the defines out, found here

Community
  • 1
  • 1
Gemini14
  • 6,246
  • 4
  • 28
  • 32
  • That's good point. TARGET_OS_IPHONE and TARGET_IPHONE_SIMULATOR defined was exist. >>Android it's ANDROID Oh, it could divide by ANDROID defined. Thanks. – Shiva Jul 06 '10 at 01:00
  • 1
    It defined it as follows. [ANDROID -> Android] [TARGET_OS_IPHONE or TARGET_IPHONE_SIMULATOR -> iPhone/iPad/iPodTouch] [WIN32 -> Windows] [When TARGET_OS_IPHONE and TARGET_IPHONE_SIMULATOR is not define and __APPLE__ is defined -> Mac] [Otherwise -> Unix or Linux] – Shiva Jul 07 '10 at 10:42
4

You could always make some up for your projects, thereby freeing yourself from compiler/platform specific defines.

  • Android: #define PLATFORM_ANDROID
  • Iphone: #define PLATFORM_IPHONE
  • etc.

And have all builds include a configuration file that defines one of these macros in a given project and that should work across the board. If you're using Visual Studio, you can just define these in the Project Settings without even needing a configuration file.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Zoli
  • 1,137
  • 7
  • 12
  • 2
    This is really the only safe, clean way to do it. I've seen the alternative in several programs (GNU screen is the worst I can think of) and it becomes a nightmare. Not to mention, if your code is testing any macro definition that begins with _ which isn't explicitly defined in the C standard, it has unspecified behavior. Aside from this, you really should avoid testing for platforms and assuming presence/absence of features based on the platform. Platforms change a lot over time. It's much better to test for specific features you want to use and have something like `#ifdef HAVE_FOO`. – R.. GitHub STOP HELPING ICE Jul 03 '10 at 10:27
  • Uh, huh. I see. When development in linux OS like ubuntu, the way seems to be good. Thanks! – Shiva Jul 06 '10 at 01:34
2

Android NDK uses definition ANDROID.

ognian
  • 11,451
  • 4
  • 35
  • 33