使用ifndef和||进行条件编译 不会遇到第二种情况

the*_*ner 8 objective-c conditional-compilation ios

我正在尝试在设置两个定义中的一个或两个时禁用自动崩溃日志报告:DEBUG对于我们的调试版本和INTERNATIONAL国际版本.#ifndef但是,当我尝试在这种情况下执行此操作时,我会收到警告Extra tokens at end of #ifndef directive并使用已DEBUG定义的运行触发Crittercism.

#ifndef defined(INTERNATIONAL) || defined(DEBUG)
    // WE NEED TO REGISTER WITH THE CRITTERCISM APP ID ON THE CRITTERCISM WEB PORTAL
    [Crittercism enableWithAppID:@"hahayoudidntthinkidleavetherealonedidyou"];
#else
    DDLogInfo(@"Crash log reporting is unavailable in the international build");

    // Since Crittercism is disabled for international builds, go ahead and
    // registers our custom exception handler. It's not as good sadly
    NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler);
    DDLogInfo(@"Registered exception handler");
#endif
Run Code Online (Sandbox Code Playgroud)

这个真值表显示了我的期望:

INTL defined | DEBUG defined | Crittercism Enabled
     F       |      F        |    T
     F       |      T        |    F
     T       |      F        |    F
     T       |      T        |    F
Run Code Online (Sandbox Code Playgroud)

这只是在它刚刚发挥作用之前#ifndef INTERNATIONAL.我也试过defined(blah)在整个语句周围没有括号和括号(分别是相同的警告和错误).

如何从编译器中获得我想要的行为?

rma*_*ddy 15

你要:

#if !defined(INTERNATIONAL) && !defined(DEBUG)
    // neither defined - setup Crittercism
#else
    // one or both defined
#endif
Run Code Online (Sandbox Code Playgroud)

或者你可以这样做:

#if defined(INTERNATIONAL) || defined(DEBUG)
    // one or both defined
#else
    // neither defined - setup Crittercism
#endif
Run Code Online (Sandbox Code Playgroud)

  • 你不能将 `#ifdef` 或 `#ifndef` 与 `defined()` 结合使用。而`#ifdef` 和`#ifndef` 只能检查一个值——`#ifndef INTERNATIONAL`。 (2认同)