![]() | Ned Batchelder : Blog | Code | Text | Site Showing C header structure » Home : Blog : February 2004 |
Showing C header structureTuesday 3 February 2004 A co-worker had a mysterious C++ compile error: a symbol was undefined, but it was clearly present in the header file. We figured there was an ifdef affecting it, but how to find the culprit in the 11,000-line header file? I tossed off this Python script: # showifstruct.py Give it a file name and a line number, and it will track what preprocessor directives affect the line in question. It prints the lines that matter, indented to show structure, with line numbers: $ showifstruct WinUser.h 11100 | |
Comments
Ah--very nice indeed! I had a problem the other day that this would have helped with.
Cool. Years ago at my first startup one of our senior developers became enamoured with C macros. Way, way, way too enamoured. He wrote nearly all of a very complex math package with C macros. The application developers were screaming in agony trying to debug the damn things when things went wrong. At least with C++ inlines you can usually disable the inlining for debugging purposes.
At which point I once again think "I really must do some coding in Python some time."
Or you could use gcc's -E option (/E in MSVC) to see the preprocessor output...
Yes, MSVC has the -E option as well, but it doesn't help much: it would show that in fact the line you wanted was precompiled away, but you are still stuck trying to figure out why. In fact, after -E, it may be even harder to tell why, since the symbols in the #ifdefs will have been replaced with their values.
Very nice, I do like browsing clean and concise python code.
A minor nit - instead of this:
del ifstack[-1]
you could do this:
ifstack.pop()
Add a comment: