我正在尝试像Linux内核一样实现链接列表。
我认为我的头文件是好的,但是当我尝试编译我的主文件来testing我做了什么,我真的不明白什么是错的…
我的文件list.h:
#ifndef _LIST_H #define _LIST_H struct list_head { struct list_head *next,*prev; }; static inline void INIT_LIST_HEAD (struct list_head *head) { head->next = head->prev = head; } static inline void list_add (struct list_head *node,struct list_head *head) { head->next->prev = node; node->next = head->next; node->prev = head; head->next = node; } static inline void list_del (struct list_head *node) { node->next->prev = node->prev; node->prev->next = node->next; } #define container_of(ptr,type,member) ({ const typeof( ((type *)0)->member ) *__mptr = (ptr); (type *)( (char *)__mptr - offsetof(type,member) );}) #define list_for_each_entry(cur,head,member) for (cur = container_of((head)->next,typeof(*cur),member);&cur->member != (head); cur = container_of(cur->member.next,member)) #endif /* _LIST_H */
我的主要文件:
为什么在用户程序中dynamic分配缓冲区会使内核驱动程序崩溃
Linux:好友系统可用内存
Linux内核,用户空间缓冲区,做access_ok并等待创build竞争条件?
哪个linux进程处理系统调用?
Fedora 19的x86_64内核版本与内核源码不同
#include <stdlib.h> #include <stdio.h> #include "list.h" struct kool_list{ int to; struct list_head list; int from; }; int main(){ struct kool_list *tmp; struct list_head *pos,*q; unsigned int i; struct kool_list mylist; INIT_LIST_HEAD(&mylist.list); /* adding elements to mylist */ for(i=5; i!=0; --i){ tmp= (struct kool_list *)malloc(sizeof(struct kool_list)); printf("enter to and from:"); scanf("%d %d",&tmp->to,&tmp->from); list_add(&(tmp->list),&(mylist.list)); } printf("n"); printf("traversing the list using list_for_each_entry()n"); list_for_each_entry(tmp,&mylist.list,list) printf("to= %d from= %dn",tmp->to,tmp->from); printf("n"); return 0; }
当我用gcc * .c -o main编译它时,我得到以下错误:
In file included from main.c:3:0: main.c: In function 'main': list.h:35:41: error: expected expression before 'typeof' for (cur = container_of((head)->next,member);&cur->member != (head); ^ list.h:32:37: note: in deFinition of macro 'container_of' (type *)( (char *)__mptr - offsetof(type,member) );}) ^ main.c:77:2: note: in expansion of macro 'list_for_each_entry' list_for_each_entry(tmp,list) ^ list.h:36:39: error: expected expression before 'typeof' cur = container_of(cur->member.next,member)) ^ list.h:32:37: note: in deFinition of macro 'container_of' (type *)( (char *)__mptr - offsetof(type,list) ^
看来我不是在操纵'typeof'的正确方法,但我不明白为什么……如果有人能解释什么是错的,谢谢:)
无法在虚拟地址处理内核分页请求 – 内核OOPS
在等待队列中唤醒进程
简单的C文件操作函数,如getc,putc和seek在Linux内核中可用?
点击界面不出来
内核sys_call_table地址与system.map中指定的地址不匹配
你的错误是由于缺少一个include引起的 – offsetof不是一个内建的运算符(如sizeof ), 而是一个在stddef.h定义的真正的宏 。
如果没有包含这个文件,编译器会假定它必须引用一个函数,这会导致编译器完全错误地解释语法,并给你一个不明确的错误信息(因为函数不能接受类型作为它们的参数)。
您可以通过包含stddef.h来修复即时语法错误。 通过编译尽可能多的严格的开关和警告(至少使用-Wall ),您可以更深入地了解这些情况下出现的问题。 GCC默认以相当宽松的模式进行编译,除此之外,它并不认为隐式函数声明是一个错误,所以编译器完全忽略了这个问题的根本原因。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。