Skip to content

C/C++ scanner: add __has_include #4517

Open
@mwichmann

Description

@mwichmann

A preprocessor identifier __has_include was added to the C standard in the 2023 edition; the same identifier has appeared in the C++ standard since the 2017 edition (one of the justifications for adding to C was to keep the preprocessors relatively aligned). The identifier allows coding conditional inclusion of a header file, and thus affects SCons dependency scanning. Since most C compilers are also C++ compilers, this identifier is widely implemented already.

From the standard, here is example usage:

#if __has_include(<optional.h>)
  #include <optional.h>
  #define have_optional 1
#elif __has_include(<experimental/optional.h>)
  #include <experimental/optional.h>
  #define have_optional 1
  #define have_experimental_optional 1
#endif
#ifndef have_optional
  #define have_optional 0
#endif

As a shorter write-up than going through the entire standard, here is the accepted proposal which added it to C:

https://open-std.org/JTC1/SC22/WG14/www/docs/n2799.pdf

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions