Debugging the C++ preprocessor
c++, c-preprocessor
Solution
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.
Problem
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.