ios Xcode 在为 iPhone 编译时设置了什么 #defines
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/146986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What #defines are set up by Xcode when compiling for iPhone
提问by Airsource Ltd
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...
.
我正在编写一些半便携式代码并希望能够检测到我何时为 iPhone 进行编译。所以我想要类似的东西#ifdef IPHONE_SDK...
。
Presumably Xcode defines something, but I can't see anything under project properties, and Google isn't much help.
大概 Xcode 定义了一些东西,但我在项目属性下看不到任何东西,谷歌也没有太大帮助。
回答by Airsource Ltd
It's in the SDK docs under "Compiling source code conditionally"
它位于“有条件地编译源代码”下的 SDK 文档中
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:
相关的定义是 TARGET_OS_IPHONE(他弃用了 TARGET_IPHONE_SIMULATOR),它们在 iOS 框架内的 /usr/include/TargetConditionals.h 中定义。在早期版本的工具链上,您必须编写:
#include "TargetConditionals.h"
but this is no longer necessary on the current (xCode 6/iOS8) toolchain.
但这在当前 (xCode 6/iOS8) 工具链上不再需要。
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
回答by lajos
To look at all the defined macros, add this to the "Other C Flags" of your build config:
要查看所有定义的宏,请将其添加到构建配置的“其他 C 标志”中:
-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:
您将收到一些构建错误,但编译器会将所有定义转储到项目根目录中的 .mi 文件中。您可以使用 grep 来查看它们,例如:
grep define main.mi
When you're done, don't forget to remove these options from the build setting.
完成后,不要忘记从构建设置中删除这些选项。
回答by russbishop
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:
这是条件的 ASCII 图表。由于历史原因,这些名称不一定有意义:
+--------------------------------------+
| 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_MAC 适用于所有 Apple 平台。
TARGET_OS_OSX is true only for macOS
TARGET_OS_OSX 仅适用于 macOS
TARGET_OS_IPHONE is true for iOS, watchOS, and tvOS (devices & simulators)
TARGET_OS_IPHONE 适用于 iOS、watchOS 和 tvOS(设备和模拟器)
TARGET_OS_IOS is true only for iOS (devices & simulators)
TARGET_OS_IOS 仅适用于 iOS(设备和模拟器)
TARGET_OS_WATCH is true only for watchOS (devices & simulators)
TARGET_OS_WATCH 仅适用于 watchOS(设备和模拟器)
TARGET_OS_TV is true only for tvOS (devices & simulators)
TARGET_OS_TV 仅适用于 tvOS(设备和模拟器)
TARGET_OS_EMBEDDED is true only for iOS/watchOS/tvOS hardware
TARGET_OS_EMBEDDED 仅适用于 iOS/watchOS/tvOS 硬件
TARGET_OS_SIMULATOR is true only for the Simulator.
TARGET_OS_SIMULATOR 仅适用于模拟器。
I'll also note that you can conditionalize settings in xcconfig
files by platform:
我还要注意,您可以xcconfig
按平台条件化文件中的设置:
//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*] = ...