feature_test_macros - Linux
Overview
The feature_test_macros
command is utilized during the compilation stage of a C or C++ program to ascertain whether specific language features are supported by the compiler. It enables developers to test for the availability of specific features and conditionally compile code based on the results.
Syntax
#include <features.h>
#define __STDC_VERSION__ 199410L
#define __STDC_HOSTED__ 1
#define __cplusplus 199711L
#define __linux 1
#define __unix 1
#define __unix__ 1
#define __USE_POSIX 1
#define __USE_POSIX2 1
#define __USE_POSIX199309 1
#define __USE_POSIX199506 1
#define __USE_XOPEN 1
#define __USE_XOPEN2K 1
#define __USE_XOPEN2K8 1
#define __USE_XOPEN_EXTENDED 1
#define __USE_GNU 1
#define __USE_SVID 1
#define __USE_BSD 1
#define __USE_MISC 1
#define __USE_ISOC11 1
#define __USE_ISOC99 1
#define __USE_ISOC2X 1
#define __USE_ISOC1X 1
#define __USE_ISOC95 1
#define __USE_ISOC199506 1
#define __USE_POSIX_IMPLICITLY 1
#define __USE_POSIX2_IMPLICITLY 1
#define __USE_POSIX199309_IMPLICITLY 1
#define __USE_POSIX199506_IMPLICITLY 1
#define __USE_XOPEN2K8_IMPLICITLY 1
#define __USE_XOPEN2K_IMPLICITLY 1
#define __USE_XOPEN_EXTENDED_IMPLICITLY 1
#define __USE_BSD_IMPLICITLY 1
#define __USE_MISC_IMPLICITLY 1
#define __USE_ISOC11_IMPLICITLY 1
#define __USE_ISOC99_IMPLICITLY 1
#define __USE_ISOC2X_IMPLICITLY 1
#define __USE_ISOC1X_IMPLICITLY 1
#define __USE_ISOC95_IMPLICITLY 1
#define __USE_ISOC199506_IMPLICITLY 1
#define __STDC_ISO_10646__ 201706L
#define __WORDSIZE 64
#define __BYTE_ORDER __LITTLE_ENDIAN
#define __ORDER_BIG_ENDIAN__ 4321
#define __ORDER_LITTLE_ENDIAN__ 1234
#define __ORDER_PDP_ENDIAN__ 3412
#define __FLOAT_WORD_ORDER __ORDER_LITTLE_ENDIAN__
#define __SIZEOF_INT__ 4
#define __SIZEOF_LONG__ 8
#define __SIZEOF_LONG_LONG__ 8
#define __SIZEOF_SHORT__ 2
#define __SIZEOF_FLOAT__ 4
#define __SIZEOF_DOUBLE__ 8
#define __SIZEOF_LONG_DOUBLE__ 16
#define __SIZEOF_SIZE_T__ 8
#define __SIZEOF_WCHAR_T__ 4
#define __SIZEOF_WINT_T__ 4
#define __SIZEOF_POINTER__ 8
#define __SIZE_TYPE__ long int
#define __PTRDIFF_TYPE__ long int
#define __WCHAR_TYPE__ int
#define __WINT_TYPE__ int
#define __INTPTR_TYPE__ long int
#define __UINTPTR_TYPE__ long unsigned int
Options/Flags
This command does not have any options or flags.
Examples
C Example:
#include <stdio.h>
#include <features.h>
int main() {
#if __STDC_VERSION__ >= 199901L
printf("C99 standard is supported.\n");
#else
printf("C99 standard is not supported.\n");
#endif
return 0;
}
C++ Example:
#include <iostream>
#include <features.h>
int main() {
#if __cplusplus >= 201103L
std::cout << "C++11 standard is supported." << std::endl;
#else
std::cout << "C++11 standard is not supported." << std::endl;
#endif
return 0;
}
Common Issues
-
Incorrect C/C++ Version Check: Ensure that you are using the correct version macro for feature testing. The version macros, such as
__STDC_VERSION__
and__cplusplus
, indicate the supported version of the C or C++ standard, not the version of the compiler itself. -
Missing
<features.h>
Header: Remember to include the<features.h>
header in your code to access the feature test macros.
Integration
Feature test macros can be combined with conditional compilation to selectively enable or disable code blocks based on the availability of specific features. This allows for cross-platform code development and ensures that the code executes correctly on systems with varying levels of support for different language features.
Related Commands
- cpp: C preprocessor that can perform macro expansion and conditional compilation.
- gcc: GNU C compiler.
- clang: LLVM C/C++/Objective-C compiler.