在我們的工程中,很多時候只希望在滿足一定條件的時候才進行編譯,這是就需要用到條件編譯。
#if #elif #else #endif
#if 條件1
代碼塊1
#elif 條件2
代碼塊2
...
#else
代碼塊n
#endif
用法舉例
在開發中通常會在測試、線上等服務器間切換,我們可以寫這樣一個API的條件編譯,根據情況使用條件控制不同的服務器API。
#if 0
#define API_SEVER @"http://101.8.8.251:9000" //本地test
#elif 1
#define API_SEVER @"http://124.182.199.114:9000" //線上test
#elif 0
#define API_SEVER @"http://service1.com" //正式1
#elif 0
#define API_SEVER @"http://service2.com" //正式2
#elif 0
#define API_SEVER @"" //備用
#endif
這只是一個簡單的應用,還比如有時候我們在真機和模擬器上的編碼并不是一樣,利用條件編譯就可以節省我們切換代碼的工作。
#ifdef和#ifndef
#ifdef MACRO_Define // 如果已定義MACRO_Define這個宏
代碼塊1
#else
代碼塊2
#endif
或
#ifndef MACRO_Define // 如果未定義MACRO_Define這個宏
代碼塊1
#else
代碼塊2
#endif
defined()函數
我們可以使用defined()
和#if
的組合,來代替上邊的用法。使用defined()
和#if
的組合能判斷更復雜的條件。
#if ( defined(MACRO_Define1) || (!defined(MACRO_Define2) && defined(MACRO_Define3) )
代碼塊1
#else
代碼塊2
#endif
補充
在系統庫的的TargetConditionals.h
文件中(我當前使用的iOS版本為9.3)
蘋果已經定義了一些在不同編譯器下的宏,這些宏我們在系統庫的頭文件或者其它地方我們會經常看到,在條件編譯的時候我們也可以使用這些宏,大家可以了解一下。
- 運行于Mac OS X的gcc編譯器下
/*
* gcc based compiler used on Mac OS X
*/
#if defined(__GNUC__) && ( defined(__APPLE_CPP__) || defined(__APPLE_CC__) || defined(__MACOS_CLASSIC__) )
#define TARGET_OS_MAC 1
#define TARGET_OS_WIN32 0
#define TARGET_OS_UNIX 0
#define TARGET_OS_IPHONE 1
#define TARGET_OS_IOS 1
#define TARGET_OS_WATCH 0
#define TARGET_OS_TV 0
#define TARGET_OS_SIMULATOR 1
#define TARGET_OS_EMBEDDED 0
#define TARGET_IPHONE_SIMULATOR TARGET_OS_SIMULATOR /* deprecated */
#define TARGET_OS_NANO TARGET_OS_WATCH /* deprecated */
#if defined(__ppc__)
#define TARGET_CPU_PPC 1
#define TARGET_CPU_PPC64 0
#define TARGET_CPU_68K 0
#define TARGET_CPU_X86 0
#define TARGET_CPU_X86_64 0
#define TARGET_CPU_ARM 0
#define TARGET_CPU_ARM64 0
#define TARGET_CPU_MIPS 0
#define TARGET_CPU_SPARC 0
#define TARGET_CPU_ALPHA 0
#define TARGET_RT_LITTLE_ENDIAN 0
#define TARGET_RT_BIG_ENDIAN 1
#define TARGET_RT_64_BIT 0
#ifdef __MACOS_CLASSIC__
#define TARGET_RT_MAC_CFM 1
#define TARGET_RT_MAC_MACHO 0
#else
#define TARGET_RT_MAC_CFM 0
#define TARGET_RT_MAC_MACHO 1
#endif
#elif defined(__ppc64__)
#define TARGET_CPU_PPC 0
#define TARGET_CPU_PPC64 1
#define TARGET_CPU_68K 0
#define TARGET_CPU_X86 0
#define TARGET_CPU_X86_64 0
#define TARGET_CPU_ARM 0
#define TARGET_CPU_ARM64 0
#define TARGET_CPU_MIPS 0
#define TARGET_CPU_SPARC 0
#define TARGET_CPU_ALPHA 0
#define TARGET_RT_LITTLE_ENDIAN 0
#define TARGET_RT_BIG_ENDIAN 1
#define TARGET_RT_64_BIT 1
#define TARGET_RT_MAC_CFM 0
#define TARGET_RT_MAC_MACHO 1
#elif defined(__i386__)
#define TARGET_CPU_PPC 0
#define TARGET_CPU_PPC64 0
#define TARGET_CPU_68K 0
#define TARGET_CPU_X86 1
#define TARGET_CPU_X86_64 0
#define TARGET_CPU_ARM 0
#define TARGET_CPU_ARM64 0
#define TARGET_CPU_MIPS 0
#define TARGET_CPU_SPARC 0
#define TARGET_CPU_ALPHA 0
#define TARGET_RT_MAC_CFM 0
#define TARGET_RT_MAC_MACHO 1
#define TARGET_RT_LITTLE_ENDIAN 1
#define TARGET_RT_BIG_ENDIAN 0
#define TARGET_RT_64_BIT 0
#elif defined(__x86_64__)
#define TARGET_CPU_PPC 0
#define TARGET_CPU_PPC64 0
#define TARGET_CPU_68K 0
#define TARGET_CPU_X86 0
#define TARGET_CPU_X86_64 1
#define TARGET_CPU_ARM 0
#define TARGET_CPU_ARM64 0
#define TARGET_CPU_MIPS 0
#define TARGET_CPU_SPARC 0
#define TARGET_CPU_ALPHA 0
#define TARGET_RT_MAC_CFM 0
#define TARGET_RT_MAC_MACHO 1
#define TARGET_RT_LITTLE_ENDIAN 1
#define TARGET_RT_BIG_ENDIAN 0
#define TARGET_RT_64_BIT 1
#elif defined(__arm__)
#define TARGET_CPU_PPC 0
#define TARGET_CPU_PPC64 0
#define TARGET_CPU_68K 0
#define TARGET_CPU_X86 0
#define TARGET_CPU_X86_64 0
#define TARGET_CPU_ARM 1
#define TARGET_CPU_ARM64 0
#define TARGET_CPU_MIPS 0
#define TARGET_CPU_SPARC 0
#define TARGET_CPU_ALPHA 0
#define TARGET_RT_MAC_CFM 0
#define TARGET_RT_MAC_MACHO 1
#define TARGET_RT_LITTLE_ENDIAN 1
#define TARGET_RT_BIG_ENDIAN 0
#define TARGET_RT_64_BIT 0
#elif defined(__arm64__)
#define TARGET_CPU_PPC 0
#define TARGET_CPU_PPC64 0
#define TARGET_CPU_68K 0
#define TARGET_CPU_X86 0
#define TARGET_CPU_X86_64 0
#define TARGET_CPU_ARM 0
#define TARGET_CPU_ARM64 1
#define TARGET_CPU_MIPS 0
#define TARGET_CPU_SPARC 0
#define TARGET_CPU_ALPHA 0
#define TARGET_RT_MAC_CFM 0
#define TARGET_RT_MAC_MACHO 1
#define TARGET_RT_LITTLE_ENDIAN 1
#define TARGET_RT_BIG_ENDIAN 0
#define TARGET_RT_64_BIT 1
#else
#error unrecognized GNU C compiler
#endif
- 來自于Metrowerks/Motorola的CodeWarrior編譯器
/*
* CodeWarrior compiler from Metrowerks/Motorola
*/
#elif defined(__MWERKS__)
#define TARGET_OS_MAC 1
#define TARGET_OS_WIN32 0
#define TARGET_OS_UNIX 0
#define TARGET_OS_EMBEDDED 0
#if defined(__POWERPC__)
#define TARGET_CPU_PPC 1
#define TARGET_CPU_PPC64 0
#define TARGET_CPU_68K 0
#define TARGET_CPU_X86 0
#define TARGET_CPU_MIPS 0
#define TARGET_CPU_SPARC 0
#define TARGET_CPU_ALPHA 0
#define TARGET_RT_LITTLE_ENDIAN 0
#define TARGET_RT_BIG_ENDIAN 1
#elif defined(__INTEL__)
#define TARGET_CPU_PPC 0
#define TARGET_CPU_PPC64 0
#define TARGET_CPU_68K 0
#define TARGET_CPU_X86 1
#define TARGET_CPU_MIPS 0
#define TARGET_CPU_SPARC 0
#define TARGET_CPU_ALPHA 0
#define TARGET_RT_LITTLE_ENDIAN 1
#define TARGET_RT_BIG_ENDIAN 0
#else
#error unknown Metrowerks CPU type
#endif
#define TARGET_RT_64_BIT 0
#ifdef __MACH__
#define TARGET_RT_MAC_CFM 0
#define TARGET_RT_MAC_MACHO 1
#else
#define TARGET_RT_MAC_CFM 1
#define TARGET_RT_MAC_MACHO 0
#endif
- 未知編譯器
/*
* unknown compiler
*/
#else
#if defined(TARGET_CPU_PPC) && TARGET_CPU_PPC
#define TARGET_CPU_PPC64 0
#define TARGET_CPU_68K 0
#define TARGET_CPU_X86 0
#define TARGET_CPU_X86_64 0
#define TARGET_CPU_ARM 0
#define TARGET_CPU_ARM64 0
#define TARGET_CPU_MIPS 0
#define TARGET_CPU_SPARC 0
#define TARGET_CPU_ALPHA 0
#elif defined(TARGET_CPU_PPC64) && TARGET_CPU_PPC64
#define TARGET_CPU_PPC 0
#define TARGET_CPU_68K 0
#define TARGET_CPU_X86 0
#define TARGET_CPU_X86_64 0
#define TARGET_CPU_ARM 0
#define TARGET_CPU_ARM64 0
#define TARGET_CPU_MIPS 0
#define TARGET_CPU_SPARC 0
#define TARGET_CPU_ALPHA 0
#elif defined(TARGET_CPU_X86) && TARGET_CPU_X86
#define TARGET_CPU_PPC 0
#define TARGET_CPU_PPC64 0
#define TARGET_CPU_X86_64 0
#define TARGET_CPU_68K 0
#define TARGET_CPU_ARM 0
#define TARGET_CPU_ARM64 0
#define TARGET_CPU_MIPS 0
#define TARGET_CPU_SPARC 0
#define TARGET_CPU_ALPHA 0
#elif defined(TARGET_CPU_X86_64) && TARGET_CPU_X86_64
#define TARGET_CPU_PPC 0
#define TARGET_CPU_PPC64 0
#define TARGET_CPU_X86 0
#define TARGET_CPU_68K 0
#define TARGET_CPU_ARM 0
#define TARGET_CPU_ARM64 0
#define TARGET_CPU_MIPS 0
#define TARGET_CPU_SPARC 0
#define TARGET_CPU_ALPHA 0
#elif defined(TARGET_CPU_ARM) && TARGET_CPU_ARM
#define TARGET_CPU_PPC 0
#define TARGET_CPU_PPC64 0
#define TARGET_CPU_X86 0
#define TARGET_CPU_X86_64 0
#define TARGET_CPU_68K 0
#define TARGET_CPU_ARM64 0
#define TARGET_CPU_MIPS 0
#define TARGET_CPU_SPARC 0
#define TARGET_CPU_ALPHA 0
#elif defined(TARGET_CPU_ARM64) && TARGET_CPU_ARM64
#define TARGET_CPU_PPC 0
#define TARGET_CPU_PPC64 0
#define TARGET_CPU_X86 0
#define TARGET_CPU_X86_64 0
#define TARGET_CPU_68K 0
#define TARGET_CPU_ARM 0
#define TARGET_CPU_MIPS 0
#define TARGET_CPU_SPARC 0
#define TARGET_CPU_ALPHA 0
#else
/*
NOTE: If your compiler errors out here then support for your compiler
has not yet been added to TargetConditionals.h.
TargetConditionals.h is designed to be plug-and-play. It auto detects
which compiler is being run and configures the TARGET_ conditionals
appropriately.
The short term work around is to set the TARGET_CPU_ and TARGET_OS_
on the command line to the compiler (e.g. -DTARGET_CPU_MIPS=1 -DTARGET_OS_UNIX=1)
The long term solution is to add a new case to this file which
auto detects your compiler and sets up the TARGET_ conditionals.
Then submit the changes to Apple Computer.
*/
#error TargetConditionals.h: unknown compiler (see comment above)
#define TARGET_CPU_PPC 0
#define TARGET_CPU_68K 0
#define TARGET_CPU_X86 0
#define TARGET_CPU_ARM 0
#define TARGET_CPU_ARM64 0
#define TARGET_CPU_MIPS 0
#define TARGET_CPU_SPARC 0
#define TARGET_CPU_ALPHA 0
#endif
#define TARGET_OS_MAC 1
#define TARGET_OS_WIN32 0
#define TARGET_OS_UNIX 0
#define TARGET_OS_EMBEDDED 0
#if TARGET_CPU_PPC || TARGET_CPU_PPC64
#define TARGET_RT_BIG_ENDIAN 1
#define TARGET_RT_LITTLE_ENDIAN 0
#else
#define TARGET_RT_BIG_ENDIAN 0
#define TARGET_RT_LITTLE_ENDIAN 1
#endif
#if TARGET_CPU_PPC64 || TARGET_CPU_X86_64
#define TARGET_RT_64_BIT 1
#else
#define TARGET_RT_64_BIT 0
#endif
#ifdef __MACH__
#define TARGET_RT_MAC_MACHO 1
#define TARGET_RT_MAC_CFM 0
#else
#define TARGET_RT_MAC_MACHO 0
#define TARGET_RT_MAC_CFM 1
#endif
#endif
版權聲明:出自MajorLMJ技術博客的原創作品 ,轉載時必須注明出處及相應鏈接!