4

Possible Duplicate:
C/C++ source file after preprocessing

The other day, I was assigned a task to fix a bug in a large C/C++ project developed under VC++ 8. The problem is that this code is really full of nested macros. What is the best way to understand this code? Any suggestion is appreciated.

Community
  • 1
  • 1
TonySalimi
  • 8,257
  • 4
  • 33
  • 62

2 Answers2

3

By using the compiler line option /E the build will output all of the preprocessor output to standard out. This file will include all the macros expanded along with all of the includes expanded inline. You'll need to figure out the best way to add this flag depending on how you build your project. With the output you can better see what the macro's are expanding too, but it still can be tricky to trace the output back to which macro expanded it. However, you should be able to figure that out. Reference for the command line options for VS8.0 compiler.

As pointed out in the comments, this is a duplicate and here is the better answer: https://stackoverflow.com/a/277362/192896

Community
  • 1
  • 1
James
  • 1,754
  • 14
  • 22
  • 3
    Unless the preprocessor is being used to deliberately obfuscate the code, I find that the post-preprocessor code is harder to understand than the original. It depends on how well organized and comprehensible the macros are. – Chris Dodd Dec 15 '11 at 22:32
  • @ChrisDodd: Agreed. You are totally right. Using the mentioned technique makes the code more complicated. – TonySalimi Dec 16 '11 at 18:49
  • In my opinion, using many macros makes the code more complex when they do anything more complicated then a statement, or two, max. Unfortunately there is much source out there that overly use macros and sometimes the only way to debug them is to use the preprocessor output. – James Dec 16 '11 at 23:46
3

I think you first need to understand what the macros do, and then try to understand what the code does after the macros are expanded. Hopefully each macro or small set of macros makes some kind of sense that you can figure out from the definitions and/or comments.

While you can run the code through the preprocessor, that will generally give you a fairly undifferentiated mass (with no comments) that is probably harder to understand.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226