c - 函数“gmtime_r”的隐式声明
问题描述
我需要做什么才能gcc
包含gmtime_r(3)
from的声明time.h
?看着/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 */
解决方案
正确的方法是:
#define _POSIX_C_SOURCE 200112L
#include <time.h>
此方法明确指出源文件所需的功能,使其自包含。它还可以移植到使用任何编译器的任何 POSIX 系统。不需要特殊的编译器标志。
使用 GCC,此代码将使用 -std=c89、-std=c99 或任何其他值进行编译。重要的是什么 -std=gnu?? 默认情况下启用可能会因版本或平台而异。
有关详细信息,请参阅功能测试宏。
推荐阅读
- c++ - c++如何评估赋值运算符(if语句)
- python - 带有日期时间索引的 Pandas Pivot
- javascript - 如何在Javascript中过滤对象数组
- javascript - 通过右键单击、Applescript 和/或 Javascript 访问浏览器中的链接?
- jquery - 如何设置 jquery addClass/removeClass
- java - 在 RxJava 2.x 中使用 flatMap
- php - 在 docker 上运行的 PHP 5.6 无法设置 memcached.serializer="igbinary"
- r - dplyr / left_join 中的嵌套管道链
- javascript - Json 返回未定义,用文本替换未定义
- apple-watch - HealthKit 提高心率通知变量?