Compiler Directive to List Defined Macros?

Martin v. Loewis
Thu Jun 29 00:39:00 GMT 2000

> A few things seem to be missing though.  It doesn't show __FILE__, __LINE__,
> __func__, or __PRETTY_FUNCTION__.  I understand these are a little different
> since they are "dynamic", but it would be helpful to know which ones are
> avaliable.

The complete list can be found be combining those required by the
standard with those documented in the GCC documentation, in particular
in the section "Function Names".

> The reason I wanted to know was that I had some code like this:
>  #ifdef __func__
>    /* code using __func__ */
>  #else

As Geoff explains, __func__ is not a preprocessor macro. Instead, it
is an identifier. The proper way of testing for it is to write

#if __STDC_VERSION__+0 >= 199901L

since __func__ is defined by C99.

>  # ifdef __PRETTY_FUNCTION__
>    /* code using __PRETTY_FUNCTION__ */
>  # else
>    /* code that doewsn't use function names */
>  # endif
>  #endif

The proper way of testing for these is to write

#ifdef __GNUC__

in which case you can use either one.

>  1) This version of gcc (egcs-2.91.66 from Red Hat 6.1) doesn't support
>     __func__, but another version I used did.

Yes, I believe it was added in 2.95.

>  2) This version of gcc doesn't allow detection of __PRETTY_FUNCTION__ with
>     #ifdef or #defined.

No, that is not possible - the preprocessor has no way of knowing what
the current function is. That's why they are identifiers, or string

> But is #2 "fixed" yet?

It's not broken. It can't possibly work the way you expect it to
work. Instead, you must use other tests in portable code.


More information about the Gcc mailing list