linux中的unistd.h和sys / types.h之间的区别

时间:2021-02-24 03:14:27

When I have searched for the header unistd.h, in The Open Group, I found that it contains the standard symbolic constants & types and for sys/types.h it said for data types.

当我在The Open Group中搜索头unistd.h时,我发现它包含标准的符号常量和类型,而对于sys / types.h,它表示数据类型。

Then I found that both have uid_t, pid_t and several similar types.

然后我发现它们都有uid_t,pid_t和几种类似的类型。

I am confused why they have divided so and what are the differences between them. I have googled but I didn't get a satisfactory answers.

我很困惑为什么他们分开了,他们之间有什么区别。我用谷歌搜索,但我没有得到满意的答案。

I will be thankful if some one can give me detailed explanation.

如果有人能给我详细解释,我将感激不尽。

Thank you.

1 个解决方案

#1


7  

The division of the POSIX and C header in fine grained files probably comes from the old days when compilation might take a long time, and adding unnecesary header files made the time longer.

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

If you only need the OS types, say for the prototypes of your functions, then just #include <sys/types.h>. However if you need the function definitions, then you #include <unistd.h> or any of the other system headers, as needed.

如果您只需要操作系统类型,比如函数的原型,那么只需#include 。但是,如果需要函数定义,则根据需要#include 或任何其他系统头。

Naturally there are types that are in both headers, as you cannot declare some functions without their necessary types.

当然,两个标题中都有类型,因为如果没有必要的类型,就无法声明某些函数。

But these different declarations of the same type are guaranteed to be the same, so there is no problem if you include both.

但是这些相同类型的不同声明保证是相同的,所以如果同时包含这两种声明则没有问题。

#1


7  

The division of the POSIX and C header in fine grained files probably comes from the old days when compilation might take a long time, and adding unnecesary header files made the time longer.

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

If you only need the OS types, say for the prototypes of your functions, then just #include <sys/types.h>. However if you need the function definitions, then you #include <unistd.h> or any of the other system headers, as needed.

如果您只需要操作系统类型,比如函数的原型,那么只需#include 。但是,如果需要函数定义,则根据需要#include 或任何其他系统头。

Naturally there are types that are in both headers, as you cannot declare some functions without their necessary types.

当然,两个标题中都有类型,因为如果没有必要的类型,就无法声明某些函数。

But these different declarations of the same type are guaranteed to be the same, so there is no problem if you include both.

但是这些相同类型的不同声明保证是相同的,所以如果同时包含这两种声明则没有问题。