0

Is there a way to loop through all the included/defined header files and then #undef them all?

If looping is the issue, is there another way to #undef all of them with ease?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
tekknolagi
  • 10,663
  • 24
  • 75
  • 119
  • you mean `#undef` the header guards, not the headers themselves right? – PeterT Nov 26 '11 at 05:37
  • i mean undefine everything that has been defined – tekknolagi Nov 26 '11 at 05:38
  • and if that's what you mean, then yes :P – tekknolagi Nov 26 '11 at 05:38
  • then my answer is: not in standard C but maybe there are compiler specific directives for such a thing. May I ask why you want to do that in the first place? Btw. you can always set your build process up to use another program to pre-process your source-files before the C-Preprocessor gets to it which is what I would do if I really needed that feature – PeterT Nov 26 '11 at 05:52
  • @PeterT to screw with someone :) – tekknolagi Nov 26 '11 at 06:00
  • @PeterT how would i set that up? – tekknolagi Nov 26 '11 at 06:03
  • well what build system do you use? Pretty much every one that I know of can execute commands on the source files. Write a script that opens a file, looks for a key-phrase like `//#UNDEF_ALL_THE_THINGS` and if it finds it, recurses into the `#include`d files and finds all the `#defines` and then you output them with `#undef` in front to where the key-phrase was. You can either overwrite your file which will however be a pain in the ass to update or make a temporary changed copy that you use to compile. But that seems a little excessive for a prank :P. – PeterT Nov 26 '11 at 06:14
  • i use `gcc` straight-up... :) – tekknolagi Nov 26 '11 at 06:44
  • and if you want to write an answer, i'll accept it :P – tekknolagi Nov 26 '11 at 06:45

1 Answers1

2

GNU cpp provides a -dM directive to do exactly that, you can list all the defines:

gcc -E -dM -c foo.c

You can then use a sed script to undef them on command line :)

gcc -E -c foo.c | sed 's/#define/#undef/'

or do whatever...

have fun :)

Ahmed Masud
  • 21,655
  • 3
  • 33
  • 58
  • is there a way to do this from inside the program, though? – tekknolagi Nov 26 '11 at 08:12
  • well this is compile time stuff right... so do that and create a .h file :) include it in your program and voila – Ahmed Masud Nov 26 '11 at 08:17
  • 1
    ooh sneaky :) thank you! see http://bernsteinbear.com for some of the nefarious exploits of a programmer (parody of the XKCD exploits of a mom) and see the top post and the Python post) – tekknolagi Nov 26 '11 at 08:22
  • 1
    hilarious :) ... i think you'd have MUCH more fun if you 1) `export CPATH=-I/some/path/with/sneaky/includes` and 2) somewhere /etc/bashrc or ~/.bashrc put `alias set="set | sed -e '/CPATH/s,:*/some/path/with/sneaky/includes:*,:,g' -e 's/:::*/:/g' ` and THEN watch them struggle :) teehee – Ahmed Masud Nov 26 '11 at 08:41
  • 1
    write me a bit and i will include it and credit you as a guest author! email me at tekknolagi@gmail.com – tekknolagi Nov 26 '11 at 08:43