I'm experiencing a strange problem with some C code I am writing. Consider the following code:
#include <sys/stat.h>
ino_t inode;
According to POSIX.1-2008, the header file <sys/stat.h> defines ino_t
1:
The <sys/stat.h> header shall define the
blkcnt_t
,blksize_t
,dev_t
,ino_t
,mode_t
,nlink_t
,uid_t
,gid_t
,off_t
, andtime_t
types as described in <sys/types.h>.
This happens when I try to compile the source code above places in a file test.c on my Linux system:
$ cat test.c #include <sys/stat.h> ino_t inode; $ uname -srm Linux 3.8.0-26-generic x86_64 $ lsb_release -d Description: Ubuntu 13.04 $ gcc -c test.c $ gcc -std=c90 test.c test.c:2:1: error: unknown type name 'ino_t' $ gcc -std=c99 test.c test.c:2:1: error: unknown type name 'ino_t' $ gcc -std=c1x test.c test.c:2:1: error: unknown type name 'ino_t'
Why is the definition of ino_t
not revelead when I specify any -std option?
My manual page of fstat
says to also include sys/types.h
, and that solves the problem for me. The definition of ino_t
in sys/stat.h
is protected with the feature macros __USE_XOPEN
and __USE_XOPEN2K
. The definition in sys/types.h
is not protected that way.
The manual page also says to include unistd.h
, but that wasn't necessary to solve your problem.
According to the manual page of feature_test_macros
:
__STRICT_ANSI__
ISO Standard C. This macro is implicitly defined by gcc(1) when invoked with, for example, the-std=c99
or-ansi
flag.
I guess this means that any XOPEN features are also switched off. I could however not find any description of that.
P.S. It seems R.. (see below) feels that this is described also in the manual page of feature_test_macros
, but my limited brain is not able to find the exact wording, so I'd like to leave that as an exercise to the reader. If it were described anywhere, then I would expect it in that manual page indeed.
Beware that the gist of this answer is as follows:
Thou shall include all include files mentioned in a manual page and not try to reverse engineer which ones may not be needed.