How can C/C++ #define directives be visualised?

  • A+
Category:Languages

I am looking for ways to visualize the effects of #define's - For example that code not seen by the compiler is shown in a different color.

Primarily visual studio, but nice to know about other environments? Ideally, also code seen by the compiler, but will not be run.

 


One way is to see the source after the preprocessing, using gcc (for C) or g++ (for c++) just use the option -E

For instance having the file p.cc

#ifdef FOO # define BAR 1 #else # define BAR 2 #endif  int main() {   return BAR; } 

if I do without defining FOO I have :

/tmp % gcc -E p.cc # 1 "p.cc" # 1 "<built-in>" # 1 "<command-line>" # 1 "p.cc"  int main() {   return 2; } 

if I do defining FOO I have :

/tmp % gcc -DFOO -E p.cc # 1 "p.cc" # 1 "<built-in>" # 1 "<command-line>" # 1 "p.cc"  int main() {   return 1; } 

Note : you probably have #include in your source so after the preprocessing you get a lot of lines coming from the included files, for the question How to show 'preprocessed' code ignoring includes with GCC I put an answer to bypass them.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: