implicit declaration of function ‘typeof’

我用gcc编译,有std=c99选项。

出现
mylist.c:88: warning: implicit declaration of function ‘typeof’
mylist.c:88: error: expected expression before ‘)’ token
mylist.c:88: error: expected expression before ‘)’ token
mylist.c:91: error: expected expression before ‘)’ token
mylist.c:91: error: expected expression before ‘)’ token

感觉GCC将typeof当做了函数,而非关键字,而
list_for_each_entry 的定义为。

/**
* list_for_each_entry - iterate over list of given type
* @pos: the type * to use as a loop counter.
* @head: the head for your list.
* @member: the name of the list_struct within the struct.
*/
#define list_for_each_entry(pos, head, member) \
for (pos = list_entry((head)->next,typeof(*pos), member); \
&pos->member != (head); \
pos
= list_entry(pos->member.next, typeof(*pos), member))


mylist第88行的内容
cat mylist.c |sed -n '87,95p'
       
printf("traversing the list using list_for_each_entry()\n");
list_for_each_entry(tmp,
&mylist.list, list){
printf(
"to= %d from= %d\n", tmp->to, tmp->from);
}
list_for_each_entry(tmp,
&mylist.list,list){
if(tmp->to == 2)
break;
}



这个例子也就是linux内核里面的链表遍历。

如果将std=c99选项去掉的话,就可以编译通过。

答案隐藏在C99的编译选项,由于typeof是GCC的扩展,并不在C99标准中,参见gcc的文档

http://gcc.gnu.org/onlinedocs/gcc/Typeof.html

The typeof keyword is disabled by default when building with -std=c99
as it's a GNU extension, make it known that it's an extension by using
the underscore
-enclosed variant.

The underscore
-encosed keyword is accepted by GCC without requesting
extensions to the C99 standard,
while the simpler typeof() keyword
requires GNU extensions to the C99 standard to be explicitly requested
(e.g.: by
using the -fasm option).

ICC supports the __typeof__ keyword
as well as typeof.

如果要启用该扩展,应该用编译选项 std=gnu99即可。

原文地址:https://www.cnblogs.com/westfly/p/2006681.html