tags:

views:

300

answers:

4

Hi,

I've got the following code:

#define checkLiteMessage    \
{   \
    #ifdef LITE_VERSION \
    if (alertView.tag == 100) \
    { \
     if (buttonIndex == 1) \
     [ [UIApplication sharedApplication] openURL: buyAppLink]; \
     [alertView release]; \
     return; \
    } \
    #endif \
}   \

What I want to do is to have the following code to be included every time I call checkLiteMessage:

    #ifdef LITE_VERSION
    if (alertView.tag == 100)
    {
     if (buttonIndex == 1)
     [ [UIApplication sharedApplication] openURL: buyAppLink];
     [alertView release];
     return;
    }
    #endif

What's my problem? Why doesn't this code compile?

Thanks.

+7  A: 

You've specified the macro with line continuations, which is correct. However, that means the #ifdef statement is not at the start of the line, so the preprocessor isn't going to do it.

You can't have the #ifdef embedded inside the macro. You could reverse that:

#ifdef LITE_VERSION
#define checkLiteMessage  do {   \
    if (alertView.tag == 100)   \
    {   \
        if (buttonIndex == 1)   \
        [ [UIApplication sharedApplication] openURL: buyAppLink];       \
        [alertView release];    \
        return; \
    }   \ 
} while(0)
#else
#define checkLiteMessage do { ; } while (0)
#endif

I'll add that putting a 'return' statement inside a macro is pretty evil and will be confusing to everyone. Don't do it.

Chris Arguin
+1, sure good way to solve that :)
Johannes Schaub - litb
Speed typing courses should be mandatory. Well done.
RaphaelSP
+1  A: 

The the backslash-linefeed combination is equivalent to no letters at all so

#define x   y \
   z

is equivalent to

#define x y z

Also, according to the standard, preprocessor directives in a macro body are not expanded or interpreted and that #ifdef is passed to the compiler instead of conditionally dropping the code in between.

You may inspect this behaviour creating a file a.c with the contents

#define x y \
     z

#define v  \
       #ifdef E

x

v

and preprocessing it with the command gcc -E a.c.

In your situation, what you need to do is

 #ifdef LITE_VERSION 
 #define checkLiteMessage    \
 {   \
     if (alertView.tag == 100)   \
     {   \
         if (buttonIndex == 1)   \
         [ [UIApplication sharedApplication] openURL: buyAppLink];       \
         [alertView release];    \
         return; \
     }   \
 }   
 #else
 #define checkLiteMessage
 #endif
Adrian Panasiuk
A: 

The preprocessor is one-pass only. So the sequence #ifdef is getting to your compiler, causing an error. Make it a function.

eduffy
No it's not one-pass only, preprocessor directives are not expanded but any macros are expanded until reaching a name the second time during a particular expansion.
Adrian Panasiuk
+2  A: 

The problem is that you cannot include preprocessor directives in a macro. You may want to do that instead :

#ifdef LITE_VERSION
#define checkLiteMessage    \
{   \
    if (alertView.tag == 100)   \
    {   \
        if (buttonIndex == 1)   \
        [ [UIApplication sharedApplication] openURL: buyAppLink];       \
        [alertView release];    \
        return; \
    }   \
}
#else
#define checkLiteMessage
#endif

Also make sure that the "return" line does what you think it does (here, it exits the function calling checkLiteMessage, not just the macro (a macro cannot be "exited").

RaphaelSP