解决的timespec的重新定义在time.h中 [英] resolving redefinition of timespec in time.h
问题描述
我写一个程序,它包括 /usr/include/linux/time.h
和 /usr/include/stdlib.h。
I am writing a program which includes both /usr/include/linux/time.h
and /usr/include/stdlib.h.
问题是:
文件stdlib.h
包括 /usr/include/time.h
,它定义了结构的timespec
和 /usr/include/linux/time.h
还定义了一个。这就引入了重新定义的编译错误。
stdlib.h
includes /usr/include/time.h
, which defines 'struct timespec'
, and /usr/include/linux/time.h
also defines one. This introduces a compilation error of redefinition.
我检查'结构的timespec
的定义,在这两个头文件:
I've examined the definitions of 'struct timespec'
in these two header files:
在/usr/include/time.h:
in /usr/include/time.h:
struct timespec
{
__time_t tv_sec; /* Seconds. */
long int tv_nsec; /* Nanoseconds. */
};
在/usr/include/linux/time.h:
in /usr/include/linux/time.h:
struct timespec {
__kernel_time_t tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
};
看来,这些定义确实等价的,但我不能证明这一点。
It seems that these definitions are indeed equivalent, but I can't prove it.
我的问题是:有没有解决这个重新定义一个可靠的方法。
My question is: is there a robust way to resolve this redefinition?
其他有关这个问题的讨论也非常AP preciated。谢谢你。
Links to discussions on this problem are also highly appreciated. Thanks.
推荐答案
要解决双重定义错误的一种方法是重命名这些定义之一:
One way to resolve the double-definition error is to rename one of these definitions:
#include <time.h>
#define timespec linux_timespec
#include <linux/time.h>
#undef timespec
和则断言在编译时,这两个定义具有相同的布局:
And then assert at compile time that both definitions have the same layout:
typedef int assert_same_size[sizeof(struct linux_timespec) == sizeof(timespec) ? 1 : -1];
typedef int assert_same_alignment[__alignof(struct linux_timespec) == __alignof(timespec) ? 1 : -1];
typedef int assert_same_tv_sec[offsetof(struct linux_timespec, tv_sec) == offsetof(struct timespec, tv_sec) ? 1 : -1];
typedef int assert_same_tv_nsec[offsetof(struct linux_timespec, tv_nsec) == offsetof(struct timespec, tv_nsec) ? 1 : -1];
这篇关于解决的timespec的重新定义在time.h中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!