Tuesday, April 21, 2009

Macro expansion and Assembly code

We know that compiler will expand macros before it actually compiles the code. Sometimes it's useful if we can view the result of the expansion, especially when we use macro to implement some functions. Here is how:
Microsoft C++ compiler:
cl.exe source.cpp /E (preprocess to standard output)
cl.exe source.cpp /P (preprocess to file)
gcc:
gcc source.cpp -E (short for expand)



We can also peek into the assembly code generated by the compiler. With the following options, the compiler will generate a source.s file containing the assembly code in current directory.
Microsoft C++ compiler:
cl.exe source.cpp /FAs
gcc:
gcc source.cpp -S


References:
GCC options you should know
Macro Expansion Algorithm
Compiler Options (MSDN)

No comments: