18

What is the purpose of the features.h header? Why and when can it be used in my code?

Does it define source features supported by the system? Or does it define some additional things which must be defined depending on other defines?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
olegst
  • 1,209
  • 1
  • 13
  • 33

4 Answers4

9

The features.h header file provides various macro definitions that indicate standard conformance to other header files, i.e. which features (hence the name) should be turned on or off depending on which standard the user wishes to use.

Most C/C++ compilers have command line options to handle standards conformance. Let's take GCC as an example: when you pass the -std=gnu9x option, you ask for the GNU dialect of the C99 standard. The features.h header makes sure that all other headers that include it will turn exactly those features on or off that are needed to support this particular dialect. This is achieved by #define -ing or #undef - ing some "intermediate" macros.

As a bonus, features.h also provides the glibc version information macros as well, and various other bits & bobs.

András Aszódi
  • 8,948
  • 5
  • 48
  • 51
3

I have grepped POSIX 7 as explained at: https://unix.stackexchange.com/questions/340285/install-the-latest-posix-man-pages/483198#483198 and there are no hits for features.h, so it must be a glibc extension only.

In glibc 2.28, it is present at include/features.h.

One of the interesting things that it defines are version macros:

#include <stdio.h>
#include <features.h>

int main(void) {
  printf("__GLIBC__       %u\n", __GLIBC__);
  printf("__GLIBC_MINOR__ %u\n", __GLIBC_MINOR__);
  return 0;
}

Ubuntu 16.04, which has glibc 2.23, this outputs:

__GLIBC__       2
__GLIBC_MINOR__ 23

See also: Check glibc version for a particular gcc compiler

Also, this header seems to get included in most / all glibc headers, which might allow you to check if glibc is being used: How to tell if glibc is used but TODO I couldn't find a documentation for that.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
0

In general if you need to use any of the variables or functions defined in a header file, you need to include it in your program. This rule is valid for features.h also. You can see a URL for features.h for your reference below:

http://repo-genesis3.cbi.utsa.edu/crossref/heccer/usr/include/features.h.html

András Aszódi
  • 8,948
  • 5
  • 48
  • 51
Steephen
  • 14,645
  • 7
  • 40
  • 47
  • 5
    The question actually was: why defines in features.h and when may they need to be used, not why header files... – olegst Apr 15 '15 at 08:37
  • The problem is you can only include features.h if you already know that you are using glibc. So you need to rely on features.h being included by any of the standard ISO C header. – Johan Boulé Aug 18 '22 at 11:17
0

From features.h File Reference

Defines on whether to include algorithm variants. Less variants reduce executable size and compile time. This file is a GNU parallel extension to the Standard C++ Library.

So this file will include some algorithm listed in the reference page.

54l3d
  • 3,913
  • 4
  • 32
  • 58
  • 4
    You are referring to glibcxx's parallel/features.h, but the question was about the top-level Linux header features.h. – Geoff Romer Aug 01 '16 at 21:46