我正在写一个既包含/usr/include/linux/time.h又包含/usr/include/stdlib.h.的程序
问题是:
stdlib.h包括/usr/include/time.h,它定义了'struct timespec'‘,/usr/include/linux/time.h也定义了一个。这引入了重新定义的编译错误。
我已经检查了这两个头文件中'struct timespec'的定义:
在/usr/include/time.h中:
struct timespec
{
__time_t tv_sec; /* Seconds. */
long int tv_nsec; /* Nanoseconds. */
};在/usr/include/linux/time.h中:
struct timespec {
__kernel_time_t tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
}; 似乎这些定义确实是等价的,但我无法证明这一点。
我的问题是:有没有一种健壮的方法来解决这个重新定义?
链接到这个问题的讨论也是高度赞赏的。谢谢。
发布于 2013-12-24 19:24:58
解决双重定义错误的一种方法是重命名这些定义之一:
#include <time.h>
#define timespec linux_timespec
#include <linux/time.h>
#undef timespec然后在编译时断言这两个定义具有相同的布局:
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];发布于 2017-06-28 15:53:19
我在Ecliepse Neon IDE中遇到了同样的错误,我通过在C/C++ Build -> Setting中添加了-DHAVE_STRUCT_TIMESPEC来解决这个问题。-> -> GCC C++编译器->杂项->Other标志

https://stackoverflow.com/questions/20759750
复制相似问题