linux中ssize_t定义在哪里?

时间:2019-03-15 20:36:10

标签: c linux types include posix

操作系统:Debian 9(Linux 4.9)

编译器:GCC 8.2

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

它在哪里定义?

3 个答案:

答案 0 :(得分:2)

ssize_tsys/types.h中定义。

the POSIX documentation

  

NAME

     

sys / types.h-数据类型

     

简介

#include <sys/types.h>
     

描述

     

标题应至少定义以下类型:

     

...

     

ssize_t

  用于字节计数或错误指示。

答案 1 :(得分:1)

https://stackoverflow.com/a/29984840/6872717):

  

细粒度文件中POSIX和C标头的划分可能   来自编译可能需要很长时间的旧时代,并且   添加不必要的头文件会延长时间。

     

如果您只需要OS类型,请说出您的原型   函数,然后只需#include <sys/types.h>。但是,如果您需要   函数定义,则您#include <unistd.h>或其他任何一个   系统标头,根据需要。

对于许多POSIX函数,要么为#include <unistd.h>,要么为包含类型的小型标头为#include <sys/types.h>

答案 2 :(得分:1)

从5.9版开始,Linux手册页记录了系统数据类型,以便您可以集中地轻松找到此信息。

只需输入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>,这是它的规范标题,并且可能是提供ssize_t的最轻的标题。但是,它由记录的任何标题提供,因此,如果您碰巧在其他标题之一中也需要定义,则只能包含该其他标题。