繁体   English   中英

函数'gmtime_r'的隐式声明

[英]implicit declaration of function ‘gmtime_r’

我要怎么做才能使gcc包括来自time.hgmtime_r(3)的声明? 纵观/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 */

正确的方法是:

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

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

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

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

实际上, gmtime_r不是ISO C99标准的一部分,它是POSIX扩展。 要启用它,请使用std=gnu99标准。

您始终可以通过应用此Pixelbeat文章中的技术来验证系统上哪些功能适用于标准系统:

有时确切地知道程序中的#defined可能会造成很大的混乱,对于glibc的“功能”宏尤其如此。 这些宏已被记录(info libc“功能测试宏”),但是使用“ cpp -dD”直接查看定义的内容要容易得多:

 $ 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 

还可以查看所有可以预定义的宏:

 $ cpp -dM /dev/null 

另请注意,对于特定于编译器的宏,您可以执行以下操作:

 $ gcc -E -dM - < /dev/null 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM