简体   繁体   English

Linux下/ usr / include下的各种unistd.h有什么区别?

[英]What is the difference between the various unistd.h under /usr/include in Linux?

Under the /usr/include directory in Linux i entered the command: find -type f -name unistd.h which gave the following output: 在Linux下的/ usr / include目录下,我输入了命令:find -type f -name unistd.h,它给出了以下输出:

./unistd.h ./linux/unistd.h ./asm-generic/unistd.h ./bits/unistd.h ./asm/unistd.h ./sys/unistd.h ./unistd.h ./linux/unistd.h ./asm-generic/unistd.h ./bits/unistd.h ./asm/unistd.h ./sys/unistd.h

my question is, what is the purpose of each unistd.h, since there is only one definiton of that file in the single unix specification ? 我的问题是,每个unistd.h的目的是什么,因为单个unix规范中只有一个该文件的定义?

Thanks in advance. 提前致谢。

linux/unistd.h actually points to asm/unistd.h , which in turn points to either asm/unistd_32.h or asm/unistd_64.h , which is where system call numbers are defined and presented to user space depending on the system's architecture. linux/unistd.h实际指向asm/unistd.h ,后者又指向asm/unistd_32.hasm/unistd_64.h ,这是系统调用号被定义并呈现给用户空间的位置,具体取决于系统的体系结构。 These come from the kernel. 这些来自内核。

bits/unistd.h is a collection of macros that augment unistd.h (mostly stuff to help prevent buffer overflows), which is conditionally included via: bits/unistd.h是一个宏的集合,用于扩充unistd.h (主要用于帮助防止缓冲区溢出),有条件地包括:

/* Define some macros helping to catch buffer overflows.  */
#if __USE_FORTIFY_LEVEL > 0 && defined __extern_always_inline
# include <bits/unistd.h>
#endif

In essence, the only POSIX required header is in fact, just unistd.h , the rest are either extensions, or definitions from the kernel. 本质上,唯一的POSIX必需标头实际上只是unistd.h ,其余的是扩展或内核定义。 So, just including unistd.h is all you have to worry about doing, everything you need will be pulled in depending on your architecture and whatever build options you've selected. 因此,只需要包含unistd.h就可以完成所有工作,根据您的体系结构和您选择的任何构建选项,您需要的所有内容都会被提取。

It's a common technique in C and C++ - you have a single file with the "standard" name in the "standard" place, in this case ./unistd.h, and then have that file include one or more implementation specific files, depending on preprocessor macros. 这是C和C ++中的常用技术 - 在“标准”位置有一个带有“标准”名称的文件,在本例中为./unistd.h,然后让该文件包含一个或多个特定于实现的文件,具体取决于在预处理器宏上。 If you look at almost any "standard" C or C++ header files, you will see it including other files not mentioned in any standard. 如果您查看几乎所有“标准”C或C ++头文件,您将看到它包括任何标准中未提及的其他文件。

Basically think of /usr/include/unistd.h as a smart symbolic link. 基本上将/usr/include/unistd.h视为智能符号链接。 It will point to a correct implementation depending on what your operating conditions are. 根据您的操作条件,它将指向正确的实施。

That said, it makes difficult sometimes to figure out what that correct implementation is. 也就是说,有时候很难弄清楚正确的实现是什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM