struct timeval 结构体使用浅析

时间结构体struct timeval的使用

#include 

该头文件的位置在:/usr/include/x86_64-linux-gnu/sys/time.h。事实上,该文件只是引入了#include ,并未定义结构体。

(1)struct timeval结构体定义
/usr/include/x86_64-linux-gnu/bits/types/struct_timeval.h

#include 

/* A time value that is accurate to the nearest
   microsecond but also has a range of years.  */
struct timeval
{
  __time_t tv_sec;              /* Seconds.  */
  __suseconds_t tv_usec;        /* Microseconds.  */
};

(2)__time_t__suseconds_t宏定义
/usr/include/x86_64-linux-gnu/bits/types.h

#define __SLONGWORD_TYPE        long int
#define __ULONGWORD_TYPE        unsigned long int
/* quad_t is also 64 bits.  */
#if __WORDSIZE == 64
typedef long int __quad_t;
typedef unsigned long int __u_quad_t;
#else
__extension__ typedef long long int __quad_t;
__extension__ typedef unsigned long long int __u_quad_t;
#endif

#if __WORDSIZE == 32
# define __SQUAD_TYPE           __quad_t
# define __UQUAD_TYPE           __u_quad_t
/* We want __extension__ before typedef's that use nonstandard base types
   such as `long long' in C89 mode.  */
# define __STD_TYPE             __extension__ typedef
#elif __WORDSIZE == 64
# define __SQUAD_TYPE           long int
# define __UQUAD_TYPE           unsigned long int
/* No need to mark the typedef with __extension__.   */
# define __STD_TYPE             typedef
#else
# error
#endif
#include      /* Defines __*_T_TYPE macros.  */

__STD_TYPE __TIME_T_TYPE __time_t;      /* Seconds since the Epoch.  */
__STD_TYPE __SUSECONDS_T_TYPE __suseconds_t; /* Signed count of microseconds.  */

gcc标准C语言进行了扩展,但用到这些扩展功能时,编译器会提出警告,使用__extension__关键字会告诉gcc不要提出警告。

(3)__TIME_T_TYPE__SUSECONDS_T_TYPE宏定义
/usr/include/x86_64-linux-gnu/bits/typesizes.h

#ifndef _BITS_TYPES_H
# error "Never include  directly; use  instead."
#endif

/* See  for the meaning of these macros.  This file exists so
   that  need not vary across different GNU platforms.  */

/* X32 kernel interface is 64-bit.  */
#if defined __x86_64__ && defined __ILP32__
# define __SYSCALL_SLONG_TYPE   __SQUAD_TYPE
# define __SYSCALL_ULONG_TYPE   __UQUAD_TYPE
#else
# define __SYSCALL_SLONG_TYPE   __SLONGWORD_TYPE
# define __SYSCALL_ULONG_TYPE   __ULONGWORD_TYPE
#endif

#define __TIME_T_TYPE           __SYSCALL_SLONG_TYPE
#define __SUSECONDS_T_TYPE      __SYSCALL_SLONG_TYPE

#error命令是C/C++语言的预处理命令之一,当预处理器预处理到#error命令时将停止编译并输出用户自定义的错误消息。

你可能感兴趣的:(Linux编程)