最初,我想将 struct timeval 转换为 timespec 。
起初,这似乎并不困难,因为那里提出了一个解决方案: 是否有一种标准方法可以将 struct timeval 转换为 struct timespec?
一个宏,TIMEVAL_TO_TIMESPEC
应该可以完成这项工作。
如文档(https://www.daemon-systems.org/man/TIMEVAL_TO_TIMESPEC.3.html)中所示,它只要求sys/time.h
包含在内。但是当我尝试编译时我仍然得到相同的答案:`警告:函数'TIMEVAL_TO_TIMESPEC'的隐式声明[-Wimplicit-function-declaration]
我什至尝试编译文档中给出的示例:
#include<time.h>
#include <assert.h>
#include<sys/time.h>
static void example(struct timespec *spec, time_t minutes) {
struct timeval elapsed;
(void)gettimeofday(&elapsed, NULL);
_DIAGASSERT(spec != NULL);
TIMEVAL_TO_TIMESPEC(&elapsed, spec);
/* Add the offset for timeout in minutes. */
spec->tv_sec = spec->tv_sec + minutes * 60;
}
int main(){
return 0;
}
编译时我得到:
test.c: In function ‘example’:
test.c:10:2: warning: implicit declaration of function ‘_DIAGASSERT’ [-Wimplicit-function-declaration]
_DIAGASSERT(spec != NULL);
^
test.c:11:2: warning: implicit declaration of function ‘TIMEVAL_TO_TIMESPEC’ [-Wimplicit-function-declaration]
TIMEVAL_TO_TIMESPEC(&elapsed, spec);
^
/tmp/ccqWnL9I.o: In function `example':
test.c:(.text+0x43): undefined reference to `_DIAGASSERT'
test.c:(.text+0x5b): undefined reference to `TIMEVAL_TO_TIMESPEC'
collect2: error: ld returned 1 exit status
我做错了什么?