简体   繁体   English

函数'gmtime_r'的隐式声明

[英]implicit declaration of function ‘gmtime_r’

What do I need to do to make gcc include the declaration of gmtime_r(3) from time.h ? 我要怎么做才能使gcc包括来自time.hgmtime_r(3)的声明? Looking at /usr/include/time.h , gmtime_r and localtime_r are inside #ifdef __USE_POSIX . 纵观/usr/include/time.hgmtime_rlocaltime_r都在里面#ifdef __USE_POSIX

Do I need to do something to turn __USE_POSIX on, like a command-line option? 我是否需要做一些事情来打开__USE_POSIX ,例如命令行选项? I'm running with -std=c99 now. 我现在使用-std=c99运行。 I understand that __USE_* macros are not intended to be set directly by user code. 我知道__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 */

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. 它也可以使用任何编译器移植到任何POSIX系统。 No special compiler flag is needed. 不需要特殊的编译器标志。

With GCC this code will compile with -std=c89, -std=c99, or any other value. 使用GCC时,此代码将使用-std = c89,-std = c99或任何其他值进行编译。 Importantly what -std=gnu?? 重要的是-std = gnu吗? enables by default might differ depending on version or platform. 默认情况下启用会因版本或平台而异。

See Feature Test Macros for details. 有关详细信息,请参见功能测试宏

Actually, gmtime_r is not part of the ISO C99 standard, it's a POSIX extension. 实际上, gmtime_r不是ISO C99标准的一部分,它是POSIX扩展。 To enable that, use std=gnu99 standard. 要启用它,请使用std=gnu99标准。

You can always verify which features are available for which standard on your system by applying technique from this Pixelbeat article : 您始终可以通过应用此Pixelbeat文章中的技术来验证系统上哪些功能适用于标准系统:

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. 有时确切地知道程序中的#defined可能会造成很大的混乱,对于glibc的“功能”宏尤其如此。 These macros are documented (info libc "feature test macros"), but it's much easier to see what's defined directly using "cpp -dD" like: 这些宏已被记录(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 

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 

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

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