4.2.9 __has_include

The special operator __has_include (operand) may be used in ‘#if’ and ‘#elif’ expressions to test whether the header referenced by its operand can be included using the ‘#include’ directive. Using the operator in other contexts is not valid. The operand takes the same form as the file in the ‘#include’ directive (see Include Syntax) and evaluates to a nonzero value if the header can be included and to zero otherwise. Note that that the ability to include a header doesn’t imply that the header doesn’t contain invalid constructs or ‘#error’ directives that would cause the preprocessor to fail.

The __has_include operator by itself, without any operand or parentheses, acts as a predefined macro so that support for it can be tested in portable code. Thus, the recommended use of the operator is as follows:

#if defined __has_include
#  if __has_include (<stdatomic.h>)
#    include <stdatomic.h>
#  endif
#endif

The first ‘#if’ test succeeds only when the operator is supported by the version of GCC (or another compiler) being used. Only when that test succeeds is it valid to use __has_include as a preprocessor operator. As a result, combining the two tests into a single expression as shown below would only be valid with a compiler that supports the operator but not with others that don’t.

#if defined __has_include && __has_include ("header.h")   /* not portable */
…
#endif