首页 > 解决方案 > 为什么我看到的每个编程教程都无缘无故地包含 sys/types?

问题描述

最近我一直在看很多 C 的套接字编程教程。在每个视频中,sys/types都包含头文件,但是当我运行视频中编写的没有 . 的代码时sys/types,我没有收到任何警告或错误。这个头文件有什么作用,为什么如此普遍?

标签: csystypes.h

解决方案


该文件定义了其他文件中使用的许多类型。在旧系统上,有必要在其他系统头文件之前包含它。

socket从系统调用的手册页:

SYNOPSIS
       #include <sys/types.h>          /* See NOTES */
       #include <sys/socket.h>

       int socket(int domain, int type, int protocol);

...

NOTES
       POSIX.1 does not require the inclusion of <sys/types.h>, and this
       header file is not required on Linux.  However, some historical (BSD)
       implementations required this header file, and portable applications
       are probably wise to include it.

推荐阅读