函数‘gmtime_r’的隐式声明
implicit declaration of function ‘gmtime_r’
我需要做什么才能使 gcc
包含来自 time.h
的 gmtime_r(3)
的声明?查看/usr/include/time.h
,gmtime_r
和localtime_r
在#ifdef __USE_POSIX
内。
我需要做些什么来打开 __USE_POSIX
吗,比如命令行选项?我现在 运行 -std=c99
。我了解 __USE_*
宏不打算由用户代码直接设置。
/* 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 */
实际上,gmtime_r
不是 ISO C99 标准的一部分,它是 POSIX 的扩展。要启用它,请使用 std=gnu99
标准。
您始终可以通过应用 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
正确的做法是:
#define _POSIX_C_SOURCE 200112L
#include <time.h>
此方法明确指示源文件所需的功能,使其成为 self-contained。它还可以移植到使用任何编译器的任何 POSIX 系统。不需要特殊的编译器标志。
使用 GCC,此代码将使用 -std=c89、-std=c99 或任何其他值进行编译。重要的是什么 -std=gnu?默认启用可能因版本或平台而异。
我需要做什么才能使 gcc
包含来自 time.h
的 gmtime_r(3)
的声明?查看/usr/include/time.h
,gmtime_r
和localtime_r
在#ifdef __USE_POSIX
内。
我需要做些什么来打开 __USE_POSIX
吗,比如命令行选项?我现在 运行 -std=c99
。我了解 __USE_*
宏不打算由用户代码直接设置。
/* 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 */
实际上,gmtime_r
不是 ISO C99 标准的一部分,它是 POSIX 的扩展。要启用它,请使用 std=gnu99
标准。
您始终可以通过应用 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
正确的做法是:
#define _POSIX_C_SOURCE 200112L
#include <time.h>
此方法明确指示源文件所需的功能,使其成为 self-contained。它还可以移植到使用任何编译器的任何 POSIX 系统。不需要特殊的编译器标志。
使用 GCC,此代码将使用 -std=c89、-std=c99 或任何其他值进行编译。重要的是什么 -std=gnu?默认启用可能因版本或平台而异。