首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用ifndef和||的条件编译不能捕获第二种情况

使用ifndef和||的条件编译不能捕获第二种情况
EN

Stack Overflow用户
提问于 2013-08-30 22:44:27
回答 2查看 11.4K关注 0票数 11

当设置了两个定义中的一个或两个时,我尝试禁用自动崩溃日志报告:DEBUG对于我们的调试版本和INTERNATIONAL为国际建筑做准备。当我尝试在#ifndefcase,然而,我得到了警告Extra tokens at end of #ifndef directive和运行DEBUG定义将触发判断主义。

#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

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

INTL defined | DEBUG defined | Crittercism Enabled
     F       |      F        |    T
     F       |      T        |    F
     T       |      F        |    F
     T       |      T        |    F

这在以前是有效的,当它只是#ifndef INTERNATIONAL..。我也试过没有defined(blah)并用括号将整个语句括起来(分别是相同的警告和错误)。

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-08-30 22:46:09

您需要:

#if !defined(INTERNATIONAL) && !defined(DEBUG)
    // neither defined - setup Crittercism
#else
    // one or both defined
#endif

或者你可以这样做:

#if defined(INTERNATIONAL) || defined(DEBUG)
    // one or both defined
#else
    // neither defined - setup Crittercism
#endif
票数 21
EN

Stack Overflow用户

发布于 2021-02-26 14:03:24

我只找到一个帖子条件Compilation哪一个可以更好地解释#if/#elif

#ifdef/#ifndef从语法级别:

  • #if constant-expression newline
  • #ifdef identifier newline
  • #ifndef identifier newline
  • #else newline
  • #elif constant-expression newline
  • #endif newline

所以在这里我们可以看到#ifndef后面必须跟'identifier',这通常是由定义的宏#define指令,或者@rmaddy说“一个值”。但是if

后面可以跟“constant- expression”,这样条件表达式defined(INTERNATIONAL) || defined(DEBUG)或者!defined(INTERNATIONAL) && !defined(DEBUG)可以使用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18535706

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档