[英]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 */
正確的方法是:
#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.