Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
当我在 The Open Group 中搜索头文件 unistd.h 时,我发现它包含标准符号常量和类型,对于 sys/types.h,它表示数据类型。
然后我发现两者都有uid_t,pid_t和几个类似的类型。
我很困惑为什么他们会如此分裂,他们之间有什么区别。我已经google了,但我没有得到满意的答案。
如果有人能给我详细的解释,我将不胜感激。
谢谢你。
POSIX 和 C 头文件在细粒度文件中的划分可能来自过去编译可能需要很长时间的时代,而添加不必要的头文件会使时间变长。
如果您只需要操作系统类型,比如函数的原型,那么只需#include <sys/types.h>. 但是,如果您需要函数定义,那么您#include <unistd.h>或任何其他系统头文件(根据需要)。
#include <sys/types.h>
#include <unistd.h>
当然,两个标头中都有一些类型,因为您不能在没有必要类型的情况下声明某些函数。
但是这些相同类型的不同声明保证是相同的,所以如果你同时包含这两个是没有问题的。