Skip to content
Tutorials Center
My learning

Topics

C Reference ยท Preprocessor

__has_include and feature tests

Reference for C23 __has_include, __has_c_attribute, __has_embed and feature test macros such as _POSIX_C_SOURCE and __STDC_NO_THREADS__.

Portable C code often needs to ask the compiler questions: does this header exist, does this attribute work, which standard am I compiling against? C23 added three operators for this, usable in #if and #elif: __has_include, __has_c_attribute and __has_embed. Older mechanisms, the feature test macros, answer similar questions about the standard library.

The operators are replaced by integer constants during preprocessing, so they can be combined with &&, || and defined. Use them to select an implementation at compile time instead of failing the build.

C
#if __has_include(<threads.h>)
#  include <threads.h>
#endif

Try it

C Does this header exist?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C Choosing an implementation by attribute support

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

C
__has_include(<header>)
__has_include("header")
__has_c_attribute(attribute-token)
__has_embed(resource-name parameters...)
Operator Result
__has_include(<h>) or ("h") 1 if the header or source file can be found by #include in that form, otherwise 0.
__has_c_attribute(name) 0 if the attribute is not supported, otherwise a date-like constant such as 202003L identifying the version of the attribute (it equals the year and month of the standard that introduced or last changed it). Works for standard attribute names; vendor attributes use a vendor::name form.
__has_embed("file" ...) __STDC_EMBED_NOT_FOUND__ (0), __STDC_EMBED_FOUND__ (1) or __STDC_EMBED_EMPTY__ (2), for use with #embed.

Errors: using these operators outside #if/#elif, or giving __has_include something that is not a header name, is a constraint violation.

Usage notes

  • Test that the operator exists first. Compilers older than C23 may lack it, and an unknown function-like name in #if is a syntax error. Test #ifdef __has_include first and place the real check inside it, in a nested #if. GCC and Clang have provided __has_include since before C23.
  • __has_include only says the file is found, not that it works. A header may exist but be unusable (for example <threads.h> on a libc that has the file but not the functions).
  • Prefer __has_include over compiler checks such as #ifdef __GNUC__: ask about the feature, not the vendor.
  • Feature test macros go the other way. Macros like _POSIX_C_SOURCE, _XOPEN_SOURCE and _GNU_SOURCE are defined by you, before the first #include, to make glibc expose POSIX or GNU declarations (for example strdup, getline and fileno under -std=c23). Defining them after any system header has no effect.
  • Optional-feature macros are defined by the implementation to say a feature is missing: __STDC_NO_THREADS__, __STDC_NO_ATOMICS__, __STDC_NO_COMPLEX__, __STDC_NO_VLA__. Test them with #ifdef. Library constants such as SIZE_MAX and INT_MAX help with size checks.
  • Version checks use __STDC_VERSION__ (see predefined macros).
  • Do not build large #if ladders inside ordinary code. Gather the tests in one configuration header that defines your own HAVE_xxx macros.