views:

1020

answers:

7

In general, I occasionally have a chain of nested macros with a few preprocessor conditional elements in their definitions. These can be painful to debug since it's hard to directly see the actual code being executed.

A while ago I vaguely remember finding a compiler (gcc) flag to expand them, but I had trouble getting this to work in practice.

+11  A: 

gcc -E will output the preprocessed source to stdout.

dvorak
+1  A: 

Debug the dissasembly with the symbols loaded.

Nemanja Trifunovic
+5  A: 

For MSVC users, you can right-click on the file/project, view the settings and change the file properties to output preprocessed source (which typically in the obj directory).

+4  A: 

This might not be applicable in your situation, but macros really do hamper debugging and often are overused and avoidable.

Can you replace them with inline functions or otherwise get rid of them all together?

Joe Schneider
Thanks; good advice. I was asking not because of a particular situation but because I had trouble in the past with this and anticipate more in the future. Much of the code I maintain (mostly not written by me) extensively uses macros, so it's still useful to be able to debug them.
Tyler
+1  A: 

You should probably start moving away form Macros and start using inline and templates.

Macros are an old tool, the right tool sometimes. As a last resort remember printf is your friend (and actually printf isn't that bad a friend when your doing multithreaded stuff)

Robert Gould
A: 

GCC and compatible compilers use the -E option to output the preprocessed source to standard out.

gcc -E foo.cpp

Sun Studio also supports this flag:

CC -E foo.cpp

But even better is -xdumpmacros. You can find more information in Suns' docs.

+1  A: 

gcc -save-temps will write out a .i (or .ii file for C++) which is the output of the C preprocessor, before it gets handed to the compiler. This can often be enlightening.

archbishop