关于/usr/include/下各种unistd的区别

版权声明:随意转载,需注明出处。by think_ycx https://blog.csdn.net/think_ycx/article/details/88621621

unistd是unix standard的简称,主要封装了一些glibc函数和系统调用相关的定义(个人理解)。具体参考这里:https://zh.wikipedia.org/wiki/Unistd.h

我在linux的/usr/include下发现了有很多unistd文件,具体说来:

/usr/include/unistd.h  1

/usr/include/linux/unistd.h 2 

/usr/include/asm/unistd.h  3

/usr/include/asm/unistd_32.h  4

/usr/include/asm/unistd_64.h 5

/usr/include/bits/unistd.h  6

1中include了6,如果编译时开了FORTIFY,那么会用6中的函数,对危险函数做一个wrapper。1 6 这些是和glibc有关的。

2中仅仅是include了3。3中根据OS的bits选择是4还是5。2 3 4 5 这些都是从kernel来的。

其中4和5中是有一对关于系统调用的define的,比如#define __NR_exit 1。如果需要用__NR_execve来代表系统调用号的话,需要 #include <linux/unistd.h>。

stackoverflow的介绍言简意赅:

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. 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:

/* 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. 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.

参考:

https://stackoverflow.com/questions/2948696/what-is-the-difference-between-the-various-unistd-h-under-usr-include-in-linux/2949262#2949262

kernel中的unistd https://elixir.bootlin.com/linux/v2.6.32/source/arch/arm/include/asm/unistd.h

猜你喜欢

转载自blog.csdn.net/think_ycx/article/details/88621621