时间结构体struct timeval
的使用
#include <sys/time.h>
该头文件的位置在:/usr/include/x86_64-linux-gnu/sys/time.h
。事实上,该文件只是引入了#include <bits/types/struct_timeval.h>
,并未定义结构体。
(1)struct timeval
结构体定义/usr/include/x86_64-linux-gnu/bits/types/struct_timeval.h
#include <bits/types.h>
/* 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 <bits/typesizes.h> /* 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 <bits/typesizes.h> directly; use <sys/types.h> instead."
#endif
/* See <bits/types.h> for the meaning of these macros. This file exists so
that <bits/types.h> 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
命令时将停止编译并输出用户自定义的错误消息。