views:

4939

answers:

2

I'm writing some semi-portable code and want to be able to detect when I'm compiling for iPhone. So I want something like #ifdef IPHONE_SDK....

Presumably xCode defines something, but I can't see anything under project properties, and google isn't much help.

+17  A: 

It's in the SDK docs under "Compiling source code conditionally"

The relevant definitions are TARGET_OS_IPHONE and TARGET_IPHONE_SIMULATOR, which will be defined provided that you

#include "TargetConditionals.h"

Since that won't exist on non apple platforms, you can wrap your include like this

#ifdef __APPLE__
   #include "TargetConditionals.h"
#endif

before including that file

So, for example, if you want to check that you are running on device, you should do

#if !(TARGET_IPHONE_SIMULATOR)
Airsource Ltd
+12  A: 

To look at all the defined macros, add this to the "Other C Flags" of your build config:

-g3 -save-temps -dD

You will get some build errors, but the compiler will dump all the defines into .mi files in your project's root directory. You can use grep to look at them, for example:

grep define main.mi

When you're done, don't forget to remove these options from the build setting.

lajos
Thanks, this was useful
Airsource Ltd
Awesome! Thanks for that.
David Sykes