[英]Linux Kernel Linked List
I'm trying to use the Linux Kernel Linked List implementation but I am unable to compile. 我正在尝试使用Linux内核链接列表实现,但无法编译。 I'm following these sources exactly with no results ( http://www.roman10.net/linux-kernel-programminglinked-list/ and http://kernelnewbies.org/FAQ/LinkedLists ) 我正在严格遵循这些资源,没有任何结果( http://www.roman10.net/linux-kernel-programminglinked-list/和http://kernelnewbies.org/FAQ/LinkedLists )
The list.h Kernel Macro for LIST_HEAD_INIT is as follows: LIST_HEAD_INIT的list.h内核宏如下:
#define LIST_HEAD_INIT(name) { &(name), &(name) }
struct Node {
int data;
struct list_head list;
};
struct Node mylinkedlist;
LIST_HEAD_INIT(&mylinkedlist.list);
void add(){
struct Node first;
first.data = 1;
first.list = LIST_HEAD_INIT(first.list);
list_add_tail(&first->list, &mylinkedlist.list);
return 0;
}
I keep getting: "error: expected identifier or '(' before '{'" 我不断收到:“错误:预期的标识符或'{'之前的'{'”
You are getting that wrong. 你错了。
First, your should LIST_HEAD(mylinkedlist) , not LIST_HEAD_INIT nor struct Node mylinkedlist. 首先,您应该LIST_HEAD(mylinkedlist) ,而不是LIST_HEAD_INIT或struct Node mylinkedlist。
mylinkedlist should be a standalone head of kernel linked list struct, it's used to link all list_head. mylinkedlist应该是内核链接列表结构的独立头,它用于链接所有list_head。
Second, you should INIT_LIST_HEAD(&first.list) , this is the way to dynamically assignment; 其次,您应该INIT_LIST_HEAD(&first.list) ,这是动态分配的方法; LIST_HEAD_INIT is used when structure is statically created at compile time. 当在编译时静态创建结构时使用LIST_HEAD_INIT 。
Last, you should list_add_tail(&first.list, &mylinkedlist) . 最后,您应该list_add_tail(&first.list,&mylinkedlist) 。
so the complete code should be: 因此完整的代码应为:
LIST_HEAD(mylinkedlist);
void add(){
struct Node first;
first.data = 1;
INIT_LIST_HEAD(&first.list);
list_add_tail(&first.list, &mylinkedlist);
}
this code work fine for me. 这段代码对我来说很好用。
I suggest you read Linux Kernel Development chapter 6, it explain this very well. 我建议您阅读Linux Kernel Development第6章,它对此做了很好的解释。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.