查找系统数据类型,以clock_t
为例
头文件位置
很多与实现有关的数据类型例如clock_t
、time_t
在头文件<sys/types.h>
中定义。
使用locate
或者find
命令查找文件
locate
模糊匹配find
完全匹配
locate types.h | grep "/usr/include"
# 或者
find /usr/include -name types.h
在头文件列表中找到我们引用的<sys/types.h>
头文件
/usr/include/x86_64-linux-gnu/sys/types.h
开始查找
查看文件,找到一行头文件引用与clock_t
相关
cat /usr/include/x86_64-linux-gnu/sys/types.h
...
#if defined __USE_XOPEN || defined __USE_XOPEN2K8
# include <bits/types/clock_t.h>
#endif
...
或者通过管道查看
$ grep clock_t types.h
# include <bits/types/clock_t.h>
在include <bits/types/clock_t.h>
中继续寻找
#include <bits/types.h>
/* Returned by `clock'. */
typedef __clock_t clock_t;
#endif
发现 clock_t
与 __clock_t
关联,并且引用了头文件 #include <bits/types.h>
,查看此文件寻找__clock_t
$ grep __clock_t types.h
__STD_TYPE __CLOCK_T_TYPE __clock_t; /* Type of CPU usage counts. */
引用了头文件<bits/typesizes.h>
,并且与__CLOCK_T_TYPE
关联
$ grep "#include" types.h
#include <features.h>
#include <bits/wordsize.h>
#include <bits/typesizes.h> /* Defines __*_T_TYPE macros. */
继续,查看<bits/typesizes.h>
,找到两部分代码
#define __CLOCK_T_TYPE __SYSCALL_SLONG_TYPE
#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
第一部分表示与宏__SYSCALL_SLONG_TYPE
关联,第二部分表示,如果是64位机器,则__SYSCALL_SLONG_TYPE
宏定义为__SQUAD_TYPE
,否则为__SLONGWORD_TYPE
,我的机器是64位,则为__SQUAD_TYPE
在文件中找不到任何其他的文件引用,也没有__SQUAD_TYPE
值的定义,但在注释中找到
/* 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. */
所以,我们返回<bits/types.h>
文件查找__SQUAD_TYPE
# define __SQUAD_TYPE long int
所以,clock_t
的类型应该是long int
,长整型。