4

I'm doing some reverse engineering on some old code that is configured by a massive tangle of #ifs and #ifdefs. I'm currently looking at the various configurations by doing the substitutions in an editor. This is both tedious and (I suspect) error-prone.

Are there some gcc preprocessor options that will preprocess the #if[def]s but leave other preprocessor directives (e.g. #define) untouched? This is for reverse engineering purposes only; I don't need to compile or otherwise use the preprocessed code.

Mark Harrison
  • 297,451
  • 125
  • 333
  • 465

2 Answers2

6

I don't know of any such functionality in gcc's preprocessor. And depending on what you're trying to do, it might not even be possible. If a #ifdef directive refers to a macro previously defined (or not) by a #define directive, and you ignore #define directives, then that's clearly not going to work.

But the unifdef command is likely to be what you're looking for. I just installed it on my Ubuntu system with sudo apt-get install unifdef; there should be similar ways to install it on other systems.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
2

If you don't need to compile it, you can use the IDE's ability to understand ifdefs. In Eclipse, head to Project > Preferences > C/C++ Build > Paths and Symbols, and define the symbols you want. Eclipse will then ignore any ifdef whose symbols was not defined.

benzaita
  • 12,869
  • 3
  • 20
  • 22
  • I'm working at the command line now, but this sounds like a great suggestion that I'll keep in mind for future use. Thanks! – Mark Harrison Jun 08 '12 at 22:13