-3

I am testing two versions of the same code (with GCC version 4.9.2 on Linux, no parameters).

Both have a #define directive, followed by an #ifdef/#endif pair further down.

Now, it turns out that the combination works properly only if the label after the initial #define starts with an underscore. Without the underscore, it works.... in a very weird way, only every third time.

In other words, this works

#define _whatever

while this doesn't:

#define whatever

Even though I know how to make the directive work, just curious - does that behavior follow any standard?

Edit: Following requests below, here's two absolutely real examples. This one prints the line "Preprocessor works":

#define _whatever
#include <stdio.h>
void main()
{
#ifdef _whatever
printf("Preprocessor works \n");
#endif
}

... and this one doesn't output anything:

#define whatever
#include <stdio.h>
void main()
{
#ifdef whatever
printf("Preprocessor works \n");
#endif
}

Yes, I am even using the word "whatever" literally - I don't think, it is defined anywhere else. But again, it's the underscore that makes the label work.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
vanhemt
  • 165
  • 10
  • 1
    You don't show what is the part of the code that works or stops working. – Iharob Al Asimi Nov 25 '16 at 11:43
  • 2
    Please provide [mcve]. – user694733 Nov 25 '16 at 11:44
  • Ok, real examples: – vanhemt Nov 25 '16 at 11:59
  • 1
    Both of those examples work just fine. – Lundin Nov 25 '16 at 12:04
  • @Lundin: As they should. Just not in my very specific case.... – vanhemt Nov 25 '16 at 12:06
  • ...which is what? – Lundin Nov 25 '16 at 12:07
  • @Lundin: the version of gcc I indicated. I'm really at a loss as to what further info to add. I'm using command line; there's only one file main.c in the folder. No parameters. I first put gcc *.c, and then run the output as ./a.out – vanhemt Nov 25 '16 at 12:08
  • I tested it in gcc 4.9.1. I find it highly unlikely that this is some compiler bug. Is this code really a copy/paste of the real code? – Lundin Nov 25 '16 at 12:10
  • I agree with you wrt compiler bugs. Just tested the literal whatever example, and it started working. But not in my original code, where I just triple-checked the labels. Guess, I need a bit more time for this - will be back. – vanhemt Nov 25 '16 at 12:19
  • Adding `#ifndef whatever #error FAIL #endif` (with linebreaks of course) directly after `#define whatever` should work. Start from there, and start moving this test down the file until you find exact location where it starts failing. – user694733 Nov 25 '16 at 12:49
  • @user694733: Noted - thanks. – vanhemt Nov 25 '16 at 12:53
  • @vanhemt Also, examine the *entire* preprocessed output from gcc. `gcc -E input.c -o input.i`. See http://stackoverflow.com/questions/3917316/gcc-preprocessor – Andrew Henle Nov 25 '16 at 13:04

2 Answers2

0

There is absolutely no requirement, in any known version of gcc, that preprocessor macros begin with an underscore.

As a general rule, preprocessor macros that begin with various combinations of underscores are reserved to the implementation, and users are advised to ignore them. So #define whatever and #ifdef whatever absolutely must work.

I agree that this is a baffling and frustrating problem. There's something strange going on, but whatever the explanation is, it's not that gcc is requiring leading underscores.

Steve Summit
  • 45,437
  • 7
  • 70
  • 103
-1

Ok, so the answer is - my sloppy command of the tools.

Specifically:

(1) I was using a header file to add/remove the #define directive

(2) I have (mindlessly) compiled the header by using "gcc *" in place of "gcc *.c"

(3) The occasional presence of the compiled *.h.gch file explains the results.

So, what seemed like erratic behavior was actually me (mindlessly) removing the *.h.gch from time to time.

Thanks everyone - I have learned a lot from all the replies.

vanhemt
  • 165
  • 10