What #defines are set up by Xcode when compiling for iPhone

余生颓废 提交于 2019-11-26 11:51:11

问题


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.


回答1:


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

The relevant definitions are TARGET_OS_IPHONE (and he deprecated TARGET_IPHONE_SIMULATOR), which are defined in /usr/include/TargetConditionals.h within the iOS framework. On earlier versions of the toolchain, you had to write:

#include "TargetConditionals.h"

but this is no longer necessary on the current (xCode 6/iOS8) toolchain.

So, for example, if you want to only compile a block of code if you are building for the device, then you should do

#if !(TARGET_OS_SIMULATOR)
...
#endif



回答2:


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.




回答3:


The answers to this question aren't quite correct. The question of the platform and hardware vs Simulator is orthogonal.

Do not use architecture as a shortcut for platform or simulator detection! This kind of sloppy thinking has caused many, many programmers a great deal of heartburn and headache over the years.

Here is an ASCII chart of the conditionals. The names don't necessarily make sense for historical reasons:

+--------------------------------------+
|             TARGET_OS_MAC            |
| +---+  +---------------------------+ |
| |   |  |      TARGET_OS_IPHONE     | |
| |OSX|  | +-----+ +----+ +-------+  | |
| |   |  | | IOS | | TV | | WATCH |  | |
| |   |  | +-----+ +----+ +-------+  | |
| +---+  +---------------------------+ |
+--------------------------------------+

Devices:      TARGET_OS_EMBEDDED
Simulators:   TARGET_OS_SIMULATOR

TARGET_OS_MAC is true for all Apple platforms.


TARGET_OS_OSX is true only for macOS

TARGET_OS_IPHONE is true for iOS, watchOS, and tvOS (devices & simulators)


TARGET_OS_IOS is true only for iOS (devices & simulators)

TARGET_OS_WATCH is true only for watchOS (devices & simulators)

TARGET_OS_TV is true only for tvOS (devices & simulators)


TARGET_OS_EMBEDDED is true only for iOS/watchOS/tvOS hardware

TARGET_OS_SIMULATOR is true only for the Simulator.


I'll also note that you can conditionalize settings in xcconfig files by platform:

//macOS only
SOME_SETTING[sdk=macosx] = ...

//iOS (device & simulator)
SOME_SETTING[sdk=iphone*] = ...
//iOS (device)
SOME_SETTING[sdk=iphoneos*] = ...
//iOS (simulator)
SOME_SETTING[sdk=iphonesimulator*] = ...

//watchOS (device & simulator)
SOME_SETTING[sdk=watch*] = ...
//watchOS (device)
SOME_SETTING[sdk=watchos*] = ...
//watchOS (simulator)
SOME_SETTING[sdk=watchsimulator*] = ...

//tvOS (device & simulator)
SOME_SETTING[sdk=appletv*] = ...
//tvOS (device)
SOME_SETTING[sdk=appletvos*] = ...
//tvOS (simulator)
SOME_SETTING[sdk=appletvsimulator*] = ...

// iOS, tvOS, or watchOS Simulator
SOME_SETTING[sdk=embeddedsimulator*] = ...


来源:https://stackoverflow.com/questions/146986/what-defines-are-set-up-by-xcode-when-compiling-for-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!