Domanda

I'm trying to disable automated crash logs reports when one or both of two defines are set: DEBUG for our debug builds and INTERNATIONAL for the international builds. When I try to do that in the #ifndef case, however, I get the warning Extra tokens at end of #ifndef directive and running with DEBUG defined will trigger 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

This truth table shows what I expect:

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

This worked before when it was just #ifndef INTERNATIONAL. I've also tried without the defined(blah) and with parentheses around the whole statement (same warning and an error respectively).

How do I get the behavior I want from the compiler?

È stato utile?

Soluzione

You want:

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

Or you can do:

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

Altri suggerimenti

I just found one post Conditional Compilation which can be better explained the differences between #if/#elif and #ifdef/#ifndeffrom syntax level:

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

So here we can see #ifndef must be followed by 'identifier', that was often the macro defined by #define directive, or @rmaddy said 'a single value'.

But if can be followed by 'constant-expression' so that the conditional expression defined(INTERNATIONAL) || defined(DEBUG) or !defined(INTERNATIONAL) && !defined(DEBUG) can be used.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top