Discussion:
Problem with 'struct timespec' and C99 standard
Nickolay Pakoulin
2004-02-10 12:23:41 UTC
Permalink
Hello all!

I can not compile a program with 'struct timespec' when gcc is instructed to
follow C99 standard.

OS: RedHat 9 Cyrillic Edition
GCC: 3.2.2 and 3.3.2

Example: let's put the following lines in file t.c

#include <time.h>
struct timespec t;

Then compile it:

$ gcc -c t.c

Works fine.

$ gcc -std=c99 -c t.c
t.c:3: storage size of `t' isn't known

BUT:
$ gcc -std=gnu99 -c t.c

works!

I tried Intel compiler and it compiled successfully:
$icc -std=c99 -c t.c

struct timespec is defined in <time.h> as:
-------------------------------------------------------------------------------
#if !defined __timespec_defined && \
((defined _TIME_H && \
(defined __USE_POSIX199309 || defined __USE_MISC)) || \
defined __need_timespec)
# define __timespec_defined 1

/* POSIX.1b structure for a time value. This is like a `struct timeval' but
has nanoseconds instead of microseconds. */
struct timespec
{
__time_t tv_sec; /* Seconds. */
long int tv_nsec; /* Nanoseconds. */
};

#endif /* timespec not defined and <time.h> or need timespec. */
#undef __need_timespec
-------------------------------------------------------------------------------
The problem seems to be with __USE_POSIX199309: gcc does not define it when
-std=c99 is set. Is a gcc feature that when it follows C99 it ignores
POSIX.1b?

Nick.

PS: Please Cc: me , i am not subscribed to the list.
Eljay Love-Jensen
2004-02-10 13:57:31 UTC
Permalink
Hi Nickolay,

The time.h header file is not part of GCC, it is supplied by the OS.

The timespec structure is part of POSIX P1003.1b-1993 specification.

Does -D_POSIX_SOURCE change anything when you've -std=c99 ?

You may want to use -v to see what's going on with the command-line under the covers. Compare "gcc -v -std=c99 -c t.c" versus "gcc -v -std=gnu99 -c t.c", and see what kinds of freebie command line flags you're getting under the covers.

HTH,
--Eljay
Nickolay Pakoulin
2004-02-10 14:34:31 UTC
Permalink
Hi, Eljay!

Thanks for the answer!

Summary of the following posting:
'-D_POSIX_SOURCE' does not help
'-D_POSIX_C_SOURCE 199309L' helps

Box with Intel ompiler is not available at the moment, can not tell the
difference between gcc and icc.

intro: "EL" == Eljay Love-Jensen <***@adobe.com> writes:

EL> Hi Nickolay, The time.h header file is not part of GCC, it is supplied by
EL> the OS.

Yes, it is part of glibc (glibc-2.3 in my case)

EL> The timespec structure is part of POSIX P1003.1b-1993 specification.

EL> Does -D_POSIX_SOURCE change anything when you've -std=c99 ?

No, plain -D_POSIX_SOURCE does not help. Output:
-------------------------------------------------------------------------------
$ gcc -v -D_POSIX_SOURCE -std=c99 -c t.c
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.2.2/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking --with-system-zlib --enable-__cxa_atexit --host=i386-redhat-linux
Thread model: posix
gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5)
/usr/lib/gcc-lib/i386-redhat-linux/3.2.2/cc1 -lang-c -v -D__GNUC__=3 -D__GNUC_MINOR__=2 -D__GNUC_PATCHLEVEL__=2 -D__GXX_ABI_VERSION=102 -D__ELF__ -D__unix__ -D__gnu_linux__ -D__linux__ -D__unix -D__linux -Asystem=posix -D__NO_INLINE__ -D__STDC_HOSTED__=1 -Acpu=i386 -Amachine=i386 -D__i386 -D__i386__ -D__tune_i386__ -D_POSIX_SOURCE t.c -quiet -dumpbase t.c -std=c99 -version -o /tmp/ccg6vab1.s
GNU CPP version 3.2.2 20030222 (Red Hat Linux 3.2.2-5) (cpplib) (i386 Linux/ELF)
GNU C version 3.2.2 20030222 (Red Hat Linux 3.2.2-5) (i386-redhat-linux)
compiled by GNU C version 3.2.2 20030222 (Red Hat Linux 3.2.2-5).
ignoring nonexistent directory "/usr/i386-redhat-linux/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/local/include
/usr/lib/gcc-lib/i386-redhat-linux/3.2.2/include
/usr/include
End of search list.
t.c:4: storage size of `t' isn't known
-------------------------------------------------------------------------------

-D_POSIX_C_SOURCE 199309L works (taken from glibc manual)

$ gcc -v -D_POSIX_C_SOURCE=199309L -std=c99 -c t.c
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.2.2/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking --with-system-zlib --enable-__cxa_atexit --host=i386-redhat-linux
Thread model: posix
gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5)
/usr/lib/gcc-lib/i386-redhat-linux/3.2.2/cc1 -lang-c -v -D__GNUC__=3 -D__GNUC_MINOR__=2 -D__GNUC_PATCHLEVEL__=2 -D__GXX_ABI_VERSION=102 -D__ELF__ -D__unix__ -D__gnu_linux__ -D__linux__ -D__unix -D__linux -Asystem=posix -D__NO_INLINE__ -D__STDC_HOSTED__=1 -Acpu=i386 -Amachine=i386 -D__i386 -D__i386__ -D__tune_i386__ -D_POSIX_C_SOURCE=199309L t.c -quiet -dumpbase t.c -std=c99 -version -o /tmp/ccLQS7rJ.s
GNU CPP version 3.2.2 20030222 (Red Hat Linux 3.2.2-5) (cpplib) (i386 Linux/ELF)
GNU C version 3.2.2 20030222 (Red Hat Linux 3.2.2-5) (i386-redhat-linux)
compiled by GNU C version 3.2.2 20030222 (Red Hat Linux 3.2.2-5).
ignoring nonexistent directory "/usr/i386-redhat-linux/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/local/include
/usr/lib/gcc-lib/i386-redhat-linux/3.2.2/include
/usr/include
End of search list.
as -V -Qy -o t.o /tmp/ccLQS7rJ.s
GNU assembler version 2.13.90.0.18 (i386-redhat-linux) using BFD version 2.13.90.0.18 20030206


EL> You may want to use -v to see what's going on with the command-line under
EL> the covers. Compare "gcc -v -std=c99 -c t.c" versus "gcc -v -std=gnu99
EL> -c t.c", and see what kinds of freebie command line flags you're getting
EL> under the covers.

difference in cc1 invocation:
-std=gnu99 adds the followin defines:
-D__gnu_linux__ -Di386 -Dlinux -Dunix

Log for -std=c99
-------------------------------------------------------------------------------
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.2.2/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking --with-system-zlib --enable-__cxa_atexit --host=i386-redhat-linux
Thread model: posix
gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5)
/usr/lib/gcc-lib/i386-redhat-linux/3.2.2/cc1 -lang-c -v -D__GNUC__=3 -D__GNUC_MINOR__=2 -D__GNUC_PATCHLEVEL__=2 -D__GXX_ABI_VERSION=102 -D__ELF__ -D__unix__ -D__gnu_linux__ -D__linux__ -D__unix -D__linux -Asystem=posix -D__NO_INLINE__ -D__STDC_HOSTED__=1 -Acpu=i386 -Amachine=i386 -D__i386 -D__i386__ -D__tune_i386__ t.c -quiet -dumpbase t.c -std=c99 -version -o /tmp/ccpl8I8j.s
GNU CPP version 3.2.2 20030222 (Red Hat Linux 3.2.2-5) (cpplib) (i386 Linux/ELF)
GNU C version 3.2.2 20030222 (Red Hat Linux 3.2.2-5) (i386-redhat-linux)
compiled by GNU C version 3.2.2 20030222 (Red Hat Linux 3.2.2-5).
ignoring nonexistent directory "/usr/i386-redhat-linux/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/local/include
/usr/lib/gcc-lib/i386-redhat-linux/3.2.2/include
/usr/include
End of search list.
t.c:4: storage size of `t' isn't known
-------------------------------------------------------------------------------
Log for -std=gnu99
-------------------------------------------------------------------------------
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.2.2/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking --with-system-zlib --enable-__cxa_atexit --host=i386-redhat-linux
Thread model: posix
gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5)
/usr/lib/gcc-lib/i386-redhat-linux/3.2.2/cc1 -lang-c -v -D__GNUC__=3 -D__GNUC_MINOR__=2 -D__GNUC_PATCHLEVEL__=2 -D__GXX_ABI_VERSION=102 -D__ELF__ -Dunix -D__gnu_linux__ -Dlinux -D__ELF__ -D__unix__ -D__gnu_linux__ -D__linux__ -D__unix -D__linux -Asystem=posix -D__NO_INLINE__ -D__STDC_HOSTED__=1 -Acpu=i386 -Amachine=i386 -Di386 -D__i386 -D__i386__ -D__tune_i386__ t.c -quiet -dumpbase t.c -std=gnu99 -version -o /tmp/ccxs3CeY.s
GNU CPP version 3.2.2 20030222 (Red Hat Linux 3.2.2-5) (cpplib) (i386 Linux/ELF)
GNU C version 3.2.2 20030222 (Red Hat Linux 3.2.2-5) (i386-redhat-linux)
compiled by GNU C version 3.2.2 20030222 (Red Hat Linux 3.2.2-5).
ignoring nonexistent directory "/usr/i386-redhat-linux/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/local/include
/usr/lib/gcc-lib/i386-redhat-linux/3.2.2/include
/usr/include
End of search list.
as -V -Qy -o t.o /tmp/ccxs3CeY.s
GNU assembler version 2.13.90.0.18 (i386-redhat-linux) using BFD version 2.13.90.0.18 20030206
-------------------------------------------------------------------------------

EL> HTH, --Eljay


Nick.

Loading...