Tutorial :Can I see defined macros during compilation of a C code?



Question:

I have a piece of code which compiles without problems with x86 gcc 4.4.1 but fails with blackfin gcc 4.1.2 with many "expected unqualified-id before numeric constant" errors. I see that there are some variable names that clash with some predefined macros. Is it possible to see defined macros at a certain line of a cpp file?


Solution:1

gcc -dM -E myfile.cpp  
  • The -dM switch tells GCC to dump all the macros defined in the given file (it will include a list of macros required to be defined by the language standard as well as any additional macros GCC defines itself).

  • The -E switch tells GCC not to continue compiling after it has preprocessed the file.

In order to see a list of macros defined at a given line of a cpp file, it may be easier to first filter out any of the predefined macros (macros defined by the compiler). In BASH, you could do:

LINE=40  FILE=myfile.cpp  HEADER=myfile.h  diff <(grep -h '#include[[:space:]]*<.*>' ${FILE} ${HEADER} | gcc -dM -x c++ -E -) <(cat ${FILE} | head -n ${LINE} | gcc -x c++ -dM -E -)  

This should filter out any macros defined by standard system headers or frameworks. The extra part, -x c++, tells GCC to interpret the input as C++ source [that requires preprocessing], this is because the it won't be able to determine it based on the extension of the filename (the source code is handed to GCC via stdin).


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »