implicit declaration of function ‘typeof’

本文探讨了GCC编译器中的typeof关键字作为扩展特性,在使用std=c99选项时遇到的问题及解决方法。typeof是GCC特有的扩展,不被C99标准所包含。若在代码中使用了typeof并希望符合C99标准,则需要采取特定措施来避免编译错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我用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/archive/2011/04/06/2006681.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值