EmbDev.net

Forum: ARM programming with GCC/GNU tools #if with no expression


von Gast (Guest)


Rate this post
useful
not useful
Hi,

I've got the following code fragment and getting the error as indicated. 
I have no clue why it doesn't work, it seems to be alright for me... 
Google didn't help either, just tons of kernel compilation bug reports 
etc. but no solution. Any help is appreciated!

1
#define XYZ
2
#define WITHB
3
4
#ifdef XYZ
5
 #ifdef WITHA
6
        if (count < 1000) {
7
            save[count] = (short)IP;
8
            count++;
9
        }
10
 #elif WITHB                           //<-- ERROR: #if with no expression
11
        if (count < 1000) {
12
            save[count] = temp;
13
            count++;
14
        }
15
 #endif
16
#endif

von Guest (Guest)


Rate this post
useful
not useful
never heard of something called "elif". I just know "elsif". Try that

von Rolf Magnus (Guest)


Rate this post
useful
not useful
#elif  is not the same as a combination of #else and #ifdef, but rather 
#else and #if. And #if requires an expression, which is missing if the 
macro isn't defined.

von Ralf (Guest)


Rate this post
useful
not useful
1
 #elif WITHB                           //<-- ERROR: #if with no expression
This expression equals to
1
 #else if WITHB == TRUE                          //<-- ERROR: #if with no expression

but since you only defined WITHB (without a value) it is expanded to 0, 
which equals FALSE.

Try either to define WITHB with a value or change your if-statement as 
follows:
1
 #else ifdef WITHB

Regards,

Ralf

von Rolf Magnus (Guest)


Rate this post
useful
not useful
> ... which is missing if the macro isn't defined.

Actually I meant "which is missing if the maco is defined, but empty". 
If it's not defined, that's ok.

So try:

#define WITHB 1

von Stefan E. (sternst)


Rate this post
useful
not useful
1
#elif defined(WITHB)

von Gast (Guest)


Rate this post
useful
not useful
Wow, thats quick :-)

I tried #elsif before, but that resulted in some weired behaviour. 
Thanks to Ralf and Rolf's solution it works as intended now!

Thanks very much for your help!

1
#define WITHB 1

von Clifford S. (clifford)


Rate this post
useful
not useful
Rolf Magnus wrote:
> #elif  is not the same as a combination of #else and #ifdef, but rather
> #else and #if. And #if requires an expression, which is missing if the
> macro isn't defined.

Rolf makes a good point, but a better solution is to use the improved 
preprocessor syntax introduced in ANSI C in 1989! Despite its 20 year 
history, this K&R syntax is still prevalent; I have no idea why. This is 
what you need (already suggested by Stefan Ernst rather more succinctly 
and without the rant):
1
#define XYZ
2
#define WITHB
3
4
#if defined XYZ
5
 #if defined WITHA
6
        if (count < 1000) {
7
            save[count] = (short)IP;
8
            count++;
9
        }
10
 #elif defined WITHB
11
        if (count < 1000) {
12
            save[count] = temp;
13
            count++;
14
        }
15
 #endif
16
#endif

That way you never need to assign a value to the macros; their mere 
existence is enough.

I have no idea what "Guest" was talking about; in what language is 
#elsif valid I wonder!? Certainly non-standard, and not documented in 
the GNU C preprocessor: 
http://gcc.gnu.org/onlinedocs/cpp/Index-of-Directives.html#Index-of-Directives

Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
No account? Register here.