Declaration conflicts between time.h and linux/time.h prevent me from using CLOCK_TAI

﹥>﹥吖頭↗ 提交于 2019-12-11 08:58:00

问题


I would like to use

#include <time.h>
clock_gettime(CLOCK_TAI, &...);

but unfortunately CLOCK_TAI is not defined in stock time.h header (in openSUSE 13.2 at least). It is however defined in linux/time.h and actually supported by the operating system. But if I include the latter header, it provokes a bunch of declaration conflicts — versus both time.h and bits/types.h. Including only the linux/time.h does not help, as time.h and/or bits/types.h will be implicitly included by common headers, like unistd.h or stdlib.h, anyway.


So I tried to resolve conflicts manually. Particularly, the first compiler error message was about timespec redeclaration, so I wrote in my code:

#include <time.h>
#if defined(__timespec_defined) && !defined(_STRUCT_TIMESPEC)
    #define _STRUCT_TIMESPEC
#endif
#include <linux/time.h>

It worked, but not without yet another conflict with itimerspec redeclaration, which is declared unconditionally in both headers and is not concluded with definitions of any include guards. So I decided to ban implicit time.h inclusion altogether:

#include <linux/time.h>
#ifndef _TIME_H
    #define _TIME_H
#endif

This continued with compiler complaining about timeval redeclaration. So I banned implicit bits/types.h inclusion as well:

#include <linux/time.h>
#ifndef _TIME_H
    #define _TIME_H
#endif
#ifndef _BITS_TYPES_H
    #define _BITS_TYPES_H
#endif

Alright, but this removes important basic declarations as well, upon which common types like size_t are based. So I tried to go in the opposite direction and disable linux/types.h inclusion:

#ifndef _LINUX_TYPES_H
    #define _LINUX_TYPES_H
#endif
#include <linux/time.h>
#ifndef _TIME_H
    #define _TIME_H
#endif

As you can guess, it resulted in system-specific types like __kernel_time_t being missing, which leaded to inability to declare timespec and so on.


Thus I am wondering: is it at all possible to use linux/… headers in combination with stdlib.h and other commonly included files? Are there other ways to access system-specific CLOCK_TAI value?

来源:https://stackoverflow.com/questions/31056979/declaration-conflicts-between-time-h-and-linux-time-h-prevent-me-from-using-cloc

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!