Linux 中的 ssize_t 在哪里定义?

Where is ssize_t defined in Linux?

OS:Debian 9 (Linux 4.9)

编译器:GCC 8.2

目前我包括 <stddef.h>(其中定义了 size_t)和 <stdint.h>(其中定义了大多数整数类型),但我仍然没有 ssize_t.

在哪里定义的?

ssize_tsys/types.h 中定义。

根据 the POSIX documentation:

NAME

sys/types.h - data types

SYNOPSIS

#include <sys/types.h>

DESCRIPTION

The header shall define at least the following types:

...

ssize_t

    Used for a count of bytes or an error indication.

从 5.9 版本开始,Linux man-pages 文档系统数据类型,以便您可以集中地轻松查找这些信息。

只需输入 man ssize_t:

       ssize_t
              Include:  <sys/types.h>.   Alternatively, <aio.h>, <monetary.h>,
              <mqueue.h>, <stdio.h>, <sys/msg.h>, <sys/socket.h>, <sys/uio.h>,
              or <unistd.h>.

              Used  for a count of bytes or an error indication.  According to
              POSIX, it shall be a signed integer type capable of storing val-
              ues  at  least in the range [-1, SSIZE_MAX], and the implementa-
              tion shall support one or more  programming  environments  where
              the  width  of  ssize_t is no greater than the width of the type
              long.

              Glibc and most other implementations provide a  length  modifier
              for ssize_t for the printf(3) and the scanf(3) families of func-
              tions, which is z; resulting commonly in %zd or %zi for printing
              ssize_t  values.  Although z works for ssize_t on most implemen-
              tations, portable POSIX programs should avoid using it--for  ex-
              ample,  by converting the value to intmax_t and using its length
              modifier (j).

              Conforming to: POSIX.1-2001 and later.

              See also:  read(2),  readlink(2),  readv(2),  recv(2),  send(2),
              write(2)

              See also the ptrdiff_t and size_t types in this page.

稍后在同一页的注释部分:

NOTES
       [...]

   Conventions used in this page
       [...]

       In  "Include",  we  first  note the "primary" header(s) that define the
       type according to either the C or POSIX.1 standards.   Under  "Alterna-
       tively",  we  note  additional headers that the standards specify shall
       define the type.

如果您只想要 ssize_t,您应该包括 <sys/types.h>,这是它的规范 header,并且可能是提供 ssize_t 的最轻的一个。但是,它由任何已记录的 header 提供,因此如果您碰巧还需要其他 header 之一的定义,您可以仅包含其他 header。