簡體   English   中英

ssize_t 在 Linux 中定義在哪里?

[英]Where is ssize_t defined in Linux?

操作系統:Debian 9 (Linux 4.9)

編譯器:GCC 8.2

目前我包括<stddef.h> (定義size_t的地方)和<stdint.h> (定義了大多數整數類型的地方),但我仍然沒有ssize_t

它在哪里定義?

ssize_tsys/types.h中定義。

根據POSIX 文檔

姓名

sys/types.h - 數據類型

概要

#include <sys/types.h>

描述

標頭應至少定義以下類型:

...

ssize_t

用於字節計數或錯誤指示。

從 5.9 版開始,Linux 手冊頁記錄了系統數據類型,以便您可以集中方式輕松找到這些信息。

只需輸入man ssize_t

SSIZE_T(3type)      Linux Programmer’s Manual     SSIZE_T(3type)

NAME
       ssize_t - count of bytes or an error indication

LIBRARY
       Standard C library (libc)

SYNOPSIS
       #include <sys/types.h>

       typedef /* ... */ ssize_t;

DESCRIPTION
       Used  for  a  count of bytes or an error indication.  Ac‐
       cording to POSIX, it shall be a signed integer type capa‐
       ble  of  storing  values  at  least  in  the  range  [-1,
       SSIZE_MAX],  and  the implementation 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 functions, which is z; resulting commonly in
       %zd or %zi for printing ssize_t values.  Although z works
       for ssize_t on most implementations, portable POSIX  pro‐
       grams  should  avoid  using it—for example, by converting
       the value to intmax_t and using its length modifier (j).

VERSIONS
       <aio.h>, <mqueue.h>,  and  <sys/socket.h>  define  ssize_t
       since POSIX.1‐2008.

CONFORMING TO
       POSIX.1‐2001 and later.

NOTES
       The  following  headers  also provide this type: <aio.h>,
       <monetary.h>,   <mqueue.h>,    <stdio.h>,    <sys/msg.h>,
       <sys/socket.h>, <sys/uio.h>, and <unistd.h>.

SEE ALSO
       read(2),   readlink(2),   readv(2),   recv(2),   send(2),
       write(2), ptrdiff_t(3type), size_t(3type)

Linux                      2022‐06‐17             SSIZE_T(3type)

如果您只想要ssize_t ,則應該包含<sys/types.h> ,這是它的規范標頭,並且可能是提供ssize_t的最輕量級的標頭。 但是,它由記錄的任何標頭提供,因此如果您碰巧還需要在其他標頭之一中定義,則可以僅包含該其他標頭。

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

POSIX和C頭文件在細粒度文件中的划分可能是由於過去編譯可能需要很長時間,而添加不必要的頭文件會使時間變長。

如果僅需要OS類型,請說出您的函數原型,然后只需#include <sys/types.h> 但是,如果需要函數定義,則可以根據需要#include <unistd.h>或任何其他系統頭文件。

#include <unistd.h>用於許多POSIX函數,或者#include <sys/types.h>用於包含類型的小標頭。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM