17

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.

0

3 Answers 3

12

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.
Sign up to request clarification or add additional context in comments.

1 Comment

Seems that neither -dD nor -E output anything if there's a syntax error detected during preprocessing, which is a bit inconvenient in some cases.
3

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.

Comments

1

What's wrong with the perennial

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

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

2 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.