views:

428

answers:

3

I'm trying to build Amaya. When the build failed with

error: expected unqualified-id before ‘(’ token

I ran g++ with only the preprocessor (replacing the -c option with -E) on the file that failed to compile to see what was going on. This produced an 80,000 line file, showing me that 'Blue' had been replaced by (2 << 8), which clearly explained the error. If I correct this, the file compiles fine. I guess I could live with that, but I would like to find out why this is happening.

Is there any way I can track how the preprocessor is replacing a specific string, in this case 'Blue'?

================= Update ===================

Well, I found the culprit:

> headers=`g++ [omited for brevity] -M  \
    ../../thotlib/dialogue/AmayaClassicNotebook.cpp`

> for file in $headers ; do grep -s -H Blue $file | grep "(2 << 8)";done 

/usr/local/include/gc.h:#define Blue (2 << 8)

So adding #undef Blue fixed the problem. So using this combination of -M and grep seems OK but sometimes C++ preprocessor definitions can be a real forest; I was curious whether there were some better way, some clever GNU tool maybe.

+2  A: 

If nobody comes up with anything better (based on the source line info in the preprocessed file), you could use the -M option to get a list of headers included by the source file, and search those for "Blue". I expect it's possible for some kind of obfuscation to mean that this doesn't find what you're looking for, but normally you'll turn up the definition somewhere.

Steve Jessop
This should be covered by -E
Nathan Fellman
+1  A: 

What's wrong with the perennial

find /src -exec grep Blue {} /dev/null ';'

That usually works for me, at least as a first cut.

paxdiablo
Another place to grep is system headers like /usr/include.
laalto
In this case, the #define was in a system location, not in a file under the source directory
Alex
+2  A: 

I find running

g++ ... -dD -E $file > $file.ii

to be very useful in untangling preprocessing problems. From man g++:

-dD Dump all macro definitions, at the end of preprocessing,
    in addition to normal output.
Employed Russian