首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在time.h中解析timespec的重定义

在time.h中解析timespec的重定义
EN

Stack Overflow用户
提问于 2013-12-24 19:01:37
回答 2查看 10.6K关注 0票数 2

我正在写一个既包含/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中:

代码语言:javascript
复制
struct timespec
{
    __time_t tv_sec;            /* Seconds.  */
    long int tv_nsec;           /* Nanoseconds.  */
};

在/usr/include/linux/time.h中:

代码语言:javascript
复制
struct timespec {
    __kernel_time_t tv_sec;                 /* seconds */
    long            tv_nsec;                /* nanoseconds */
}; 

似乎这些定义确实是等价的,但我无法证明这一点。

我的问题是:有没有一种健壮的方法来解决这个重新定义?

链接到这个问题的讨论也是高度赞赏的。谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-12-24 19:24:58

解决双重定义错误的一种方法是重命名这些定义之一:

代码语言:javascript
复制
#include <time.h>
#define timespec linux_timespec
#include <linux/time.h>
#undef timespec

然后在编译时断言这两个定义具有相同的布局:

代码语言:javascript
复制
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];
票数 5
EN

Stack Overflow用户

发布于 2017-06-28 15:53:19

我在Ecliepse Neon IDE中遇到了同样的错误,我通过在C/C++ Build -> Setting中添加了-DHAVE_STRUCT_TIMESPEC来解决这个问题。-> -> GCC C++编译器->杂项->Other标志

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20759750

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档