Monday, July 18, 2011

Pre-defined preprocessor definitions (GCC)

The predef project is extremely convenient for looking up architecture-dependent, os-dependent, compiler-dependent, or standard C/C++ compiler macros, but it is not available when working offline.

To get around this, the preprocessor definitions defined automatically by GCC can be viewed with this command line:

bash$ gcc -dM < /dev/null

#define __DBL_MIN_EXP__ (-1021)
#define __UINT_LEAST16_MAX__ 65535
#define __FLT_MIN__ 1.17549435082228750797
...


This will output the #define commands as they would be executed by the preprocessor. To get a list of the non-expanded macros, use -dN :

bash$ gcc -dN < /dev/null
# 1 ""
# 1 ""
#define __STDC__
#define __STDC_HOSTED__
#define __GNUC__
#define __GNUC_MINOR__
...

This is a bit more noisy, as undefined macros (e.g. "# 1") are included, but may be more suitable for parsing.

3 comments:

  1. this does not work!
    it complains no input files

    ReplyDelete
  2. Yup, guess the GNU guys considered this a bug and "fixed it" (current version 4.5.2). Wish they'd stop doing that.

    FYI, their recommended usage is to operate on an empty file:

    touch foo.h; cpp -dM foo.h

    (GCC man page)

    ReplyDelete
  3. using C++ running under Ubuntu the syntax is:

    c++ -dM -E - < /dev/null

    ReplyDelete