求指针ptr所在的结构体实例的首地址,
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) #define container_of(ptr, type, member) ({ \ const typeof( ((type *)0)->member ) *__mptr = (ptr); \ (type *)( (char *)__mptr - offsetof(type,member) );})
编译报错
1 src/utils/dds_rbtree.c: In function ‘rbtree_get_prev’: 2 src/utils/rbtree.h:54:11: error: unknown type name ‘typeof’ 3 const typeof( ((type *)0)->member ) *__mptr = (ptr); \ 4 ^ 5 src/utils/rbtree.h:76:37: note: in expansion of macro ‘container_of’ 6 #define rb_entry(ptr, type, member) container_of(ptr, type, member) 7 ^~~~~~~~~~~~ 8 src/utils/dds_rbtree.c:377:5: note: in expansion of macro ‘rb_entry’ 9 rb_entry(pTargetBaseNode, RB_DATA_NODE_T, stRbBaseNode); 10 ^~~~~~~~ 11 src/utils/rbtree.h:54:28: error: expected ‘)’ before numeric constant 12 const typeof( ((type *)0)->member ) *__mptr = (ptr); \ 13 ^ 14 src/utils/rbtree.h:76:37: note: in expansion of macro ‘container_of’ 15 #define rb_entry(ptr, type, member) container_of(ptr, type, member) 16 ^~~~~~~~~~~~ 17 src/utils/dds_rbtree.c:377:5: note: in expansion of macro ‘rb_entry’ 18 rb_entry(pTargetBaseNode, RB_DATA_NODE_T, stRbBaseNode); 19 ^~~~~~~~ 20 src/utils/rbtree.h:54:30: error: expected ‘)’ before ‘->’ token 21 const typeof( ((type *)0)->member ) *__mptr = (ptr); \ 22 ^ 23 src/utils/rbtree.h:76:37: note: in expansion of macro ‘container_of’ 24 #define rb_entry(ptr, type, member) container_of(ptr, type, member) 25 ^~~~~~~~~~~~ 26 src/utils/dds_rbtree.c:377:5: note: in expansion of macro ‘rb_entry’ 27 rb_entry(pTargetBaseNode, RB_DATA_NODE_T, stRbBaseNode); 28 ^~~~~~~~ 29 src/utils/rbtree.h:54:41: error: expected ‘)’ before ‘*’ token 30 const typeof( ((type *)0)->member ) *__mptr = (ptr); \View Code
原因为编译是添加了c99选项,typeof为gcc扩展功能,识别为函数,c99不支持,删去--std=c99
LD_C_FLAGS += -ldl -lm -lpthread -lrt --std=c99
C++情况下参考
参考:https://blog.csdn.net/linuxsmallping/article/details/83418546
标签:src,container,utils,member,编译,rbtree,报错,type,ptr From: https://www.cnblogs.com/peifx/p/17159172.html