首页 > 解决方案 > 函数“gmtime_r”的隐式声明

问题描述

我需要做什么才能gcc包含gmtime_r(3)from的声明time.h?看着/usr/include/time.hgmtime_rlocaltime_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 */

标签: cgccposixtime.h

解决方案


正确的方法是:

#define _POSIX_C_SOURCE 200112L
#include <time.h>

此方法明确指出源文件所需的功能,使其自包含。它还可以移植到使用任何编译器的任何 POSIX 系统。不需要特殊的编译器标志。

使用 GCC,此代码将使用 -std=c89、-std=c99 或任何其他值进行编译。重要的是什么 -std=gnu?? 默认情况下启用可能会因版本或平台而异。

有关详细信息,请参阅功能测试宏


推荐阅读