EmbDev.net

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


Author: Gast (Guest)
Posted on:

Rate this post
0 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!

#define XYZ
#define WITHB

#ifdef XYZ
 #ifdef WITHA
        if (count < 1000) {
            save[count] = (short)IP;
            count++;
        }
 #elif WITHB                           //<-- ERROR: #if with no expression
        if (count < 1000) {
            save[count] = temp;
            count++;
        }
 #endif
#endif

Author: Guest (Guest)
Posted on:

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

Author: Rolf Magnus (Guest)
Posted on:

Rate this post
0 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.

Author: Ralf (Guest)
Posted on:

Rate this post
0 useful
not useful
 #elif WITHB                           //<-- ERROR: #if with no expression
This expression equals to
 #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:
 #else ifdef WITHB

Regards,

Ralf

Author: Rolf Magnus (Guest)
Posted on:

Rate this post
0 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

Author: Stefan Ernst (sternst)
Posted on:

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

Author: Gast (Guest)
Posted on:

Rate this post
0 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!

#define WITHB 1

Author: Clifford Slocombe (clifford)
Posted on:

Rate this post
0 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):
#define XYZ
#define WITHB

#if defined XYZ
 #if defined WITHA
        if (count < 1000) {
            save[count] = (short)IP;
            count++;
        }
 #elif defined WITHB
        if (count < 1000) {
            save[count] = temp;
            count++;
        }
 #endif
#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-Directi...

Reply

Entering an e-mail address is optional. If you want to receive reply notifications by e-mail, please log in.

Rules — please read before posting

  • Post long source code as attachment, not in the text
  • Posting advertisements is forbidden.

Formatting options

  • [c]C code[/c]
  • [avrasm]AVR assembler code[/avrasm]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]




Bild automatisch verkleinern, falls nötig
Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.