views:

855

answers:

3

In gcc, wow can I check what C preprocessor definitions are in place during the compilation of a C program, in particular what standard or platform-specific macrodefinitions are defined?

+12  A: 

Predefined macros depend on the standard and the way the compiler implements it.

For GCC: http://gcc.gnu.org/onlinedocs/cpp/Predefined-Macros.html

For Microsoft Visual Studio 8: http://msdn.microsoft.com/en-us/library/b0084kay(VS.80).aspx

This Wikipedia page http://en.wikipedia.org/wiki/C_preprocessor#Compiler-specific_predefined_macros lists how to dump at some of the predefined macros

CsTamas
Just to record the answer: gcc -dM -E - < /dev/null
Adrian Panasiuk
A: 

A program may define a macro at one point, remove that definition later, and then provide a different definition after that. Thus, at different points in the program, a macro may have different definitions, or have no definition at all.

Mitch Wheat
Generally true for macros, but the question was about predefined macros
CsTamas
@CsTamas: are you saying you can't undefine pre-defined macros?
Mitch Wheat
@Mitch Wheat: No, not saying that. But the question was different.
CsTamas
While your answer is true, there are certain macros that are defined by the compiler, and it would be strange for them to be undefined.
GMan
@GMan: Strange indeed. But I've seen it done!
Mitch Wheat
+3  A: 

A likely source of the predefined macros for a specific combination of compiler and platform is the Predef project at Sourceforge. They are attempting to maintain a catalog of all predefined macros in all C and C++ compilers on all platforms. In practice, they have coverage of a fair number of platforms for GCC, and a smattering of other compilers.

They achieved this through a combination of careful reading of documentation, as well as a shell script that figures out what macros are predefined the hard way: it tries them. My understanding is that it actually tries every string it can find in the executable image of the compiler and/or preprocessor to see if it has a predefined meaning.

They will happily add any info they don't have yet to their database.

RBerteig