iOS条件编译

来源:互联网 发布:英翻汉软件 编辑:程序博客网 时间:2024/06/11 17:04

转载链接:http://www.jianshu.com/p/dc0404c09060

简书作者:MajorLMJ

在我们的工程中,很多时候只希望在满足一定条件的时候才进行编译,这是就需要用到条件编译。

#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

原创粉丝点击