Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional compilation with ifndef and || doesn't catch second case

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?

like image 467
thegrinner Avatar asked Aug 30 '13 14:08

thegrinner


People also ask

What is true about conditional compilation?

Conditional compilation provides a way of including or omitting selected lines of source code depending on the values of literals specified by the DEFINE directive. In this way, you can create multiple variants of the same program without the need to maintain separate source streams.

How does Ifndef work in C?

In the C Programming Language, the #ifndef directive allows for conditional compilation. The preprocessor determines if the provided macro does not exist before including the subsequent code in the compilation process.

Can we use #else with #ifdef?

Each #if directive in a source file must be matched by a closing #endif directive. Any number of #elif directives can appear between the #if and #endif directives, but at most one #else directive is allowed. The #else directive, if present, must be the last directive before #endif.

How do you stop Ifndef?

Explanation of #ifndef in C syntax The “MACRO” definition should not be defined for the specific preprocessor which is used to include the C Programming Source Code into the specific compiled application. The #ifndef must be ended with the #endif directive of the C Programming Language.


1 Answers

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
like image 150
rmaddy Avatar answered Sep 30 '22 14:09

rmaddy