Reputation: 4313
What do I need to do to make gcc
include the declaration of gmtime_r(3)
from time.h
? Looking at /usr/include/time.h
, gmtime_r
and localtime_r
are inside #ifdef __USE_POSIX
.
Do I need to do something to turn __USE_POSIX
on, like a command-line option? I'm running with -std=c99
now. I understand that __USE_*
macros are not intended to be set directly by user code.
/* Return the `struct tm' representation of *TIMER
in Universal Coordinated Time (aka Greenwich Mean Time). */
extern struct tm *gmtime (const time_t *__timer) __THROW;
/* Return the `struct tm' representation
of *TIMER in the local timezone. */
extern struct tm *localtime (const time_t *__timer) __THROW;
#ifdef __USE_POSIX
/* Return the `struct tm' representation of *TIMER in UTC,
using *TP to store the result. */
extern struct tm *gmtime_r (const time_t *__restrict __timer,
struct tm *__restrict __tp) __THROW;
/* Return the `struct tm' representation of *TIMER in local time,
using *TP to store the result. */
extern struct tm *localtime_r (const time_t *__restrict __timer,
struct tm *__restrict __tp) __THROW;
#endif /* POSIX */
Upvotes: 3
Views: 2908
Reputation:
The proper way to do this is:
#define _POSIX_C_SOURCE 200112L
#include <time.h>
This method indicates explicitly the features that a source file requires, making it self-contained. It is also portable to any POSIX system using any compiler. No special compiler flag is needed.
With GCC this code will compile with -std=c89, -std=c99, or any other value. Importantly what -std=gnu?? enables by default might differ depending on version or platform.
See Feature Test Macros for details.
Upvotes: 9
Reputation: 31
Actually, gmtime_r
is not part of the ISO C99 standard, it’s a POSIX extension. To enable that, use std=gnu99
standard.
You can always verify which features are available for which standard on your system by applying technique from this Pixelbeat article:
It can be quite confusing sometimes to know exactly what is #defined in your program, and this is especially true for glibc's "feature" macros. These macros are documented (info libc "feature test macros"), but it's much easier to see what's defined directly using "cpp -dD" like:
$ echo "#include <features.h>" | cpp -dN | grep "#define __USE_" #define __USE_ANSI #define __USE_POSIX #define __USE_POSIX2 #define __USE_POSIX199309 #define __USE_POSIX199506 #define __USE_MISC #define __USE_BSD #define __USE_SVID $ echo "#include <features.h>" | cpp -dN -std=c99 | grep "#define __USE_" #define __USE_ANSI #define __USE_ISOC99
Also to see all the predefined macros one can do:
$ cpp -dM /dev/null
Note also for compiler specific macros you can do:
$ gcc -E -dM - < /dev/null
Upvotes: 2