最近我看了很多C的套接字编程教程。在每一个视频中,都包含头文件sys/types
,但当我在没有sys/types
的情况下运行视频中编写的代码时,我不会收到任何警告或错误。这个头文件的作用是什么?为什么它如此常见?
该文件定义了其他文件中使用的许多类型。在较旧的系统上,有必要在其他系统标头之前包含它。
来自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.