Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

__has_include

Use case

Portable header detection.

Explanation

__has_include checks if a header exists at preprocessing time.

Code

#include <iostream>

#if __has_include(<mach-o/loader.h>)
#define BINARY_FORMAT "Mach-O (macOS/iOS)"
#elif __has_include(<elf.h>)
#define BINARY_FORMAT "ELF (Linux)"
#else
#define BINARY_FORMAT "unknown"
#endif

int main() {
  std::cout << "Binary format: " << BINARY_FORMAT << "\n";
  return 0;
}

View on GitHub.

Output

$ ./src/c++17/build/has-include
Binary format: Mach-O (macOS/iOS)