Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging the C++ preprocessor

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.

like image 469
Alex Avatar asked Jul 05 '09 11:07

Alex


People also ask

Which is the C preprocessor?

The C preprocessor is the macro preprocessor for the C, Objective-C and C++ computer programming languages. The preprocessor provides the ability for the inclusion of header files, macro expansions, conditional compilation, and line control.

What is preprocessor in C example?

Examples of some preprocessor directives are: #include, #define, #ifndef etc. Remember that the # symbol only provides a path to the preprocessor, and a command such as include is processed by the preprocessor program. For example, #include will include extra code in your program.

What is Ifdef debug?

#ifdef simply tests if the symbol's been defined. #if tests the VALUE of the symbol. so #define FOO 0 will make #ifdef FOO be true, but #if FOO be false, because it's doing #if 0 .


1 Answers

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.
like image 159
Employed Russian Avatar answered Oct 01 '22 23:10

Employed Russian