最近开发时,代码使用offsetof宏,编译报错:
error: implicit declaration of function ‘offsetof’
note: ‘offsetof’ is defined in header ‘<stddef.h>’; did you forget to ‘#include <stddef.h>’?
搜索/usr/include后,只有一个linux/stddef.h,没有stddef.h,经过搜索发现,stddef.h是一个标准的c头文件,如参考1。
它提供了2个非常重要的宏:NULL和offsetof。
根据参考2,stddef.h中很多重要的类型跟编译器相关,所以这个头文件由gcc提供,而不是glibc。
在fedora上,它的位置:
/usr/lib/gcc/x86_64-redhat-linux/14/include/stddef.h
$ grep offsetof /usr/lib/gcc/x86_64-redhat-linux/14/include/stddef.h #undef offsetof /* in case a system header has defined it. */ #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
参考
【1】The Single UNIX ® Specification, Version 2
https://pubs.opengroup.org/onlinepubs/7908799/xsh/stddef.h.html
【2】
https://stackoverflow.com/questions/37158651/why-is-stddef-h-not-in-usr-include