问题
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.
Then I found that both have uid_t, pid_t and several similar types.
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:
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.
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.
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.
来源:https://stackoverflow.com/questions/29984328/difference-between-unistd-h-and-sys-types-h-in-linux