- 论坛徽章:
- 0
|
本帖最后由 感性狂野理智不灭 于 2014-04-10 18:31 编辑
一个简单的小实验程序
linux课程的一个实验
调用内核源码里的list.h的宏, 简单使用 实现链表的插入删除 求和并将其插入尾部
gcc 添加 -D__KERNEL__ 和 自己添加offset和container_of定义都不行 求解 不胜感谢
系统环境: win8.1 64位virtualbox虚拟机 ubuntu64位
错误信息:windery@windery-VirtualBox:~/linux$ gcc ex1.c -D __KERNEL__ -I/usr/src/linux-headers-3.11.0-12/include/ -o ex1
In file included from ex1.c:1:0:
ex1.c: In function ‘my_delete’:
ex1.c:33:30: error: expected expression before ‘struct’
tmp = list_entry(pos,struct node, list);
^
/usr/src/linux-headers-3.11.0-12/include/linux/list.h:351:20: note: in definition of macro ‘list_entry’
container_of(ptr, type, member)
^
ex1.c: In function ‘my_sum’:
ex1.c:56:31: error: expected expression before ‘struct’
tmp = list_entry(pos, struct node, list);
^
/usr/src/linux-headers-3.11.0-12/include/linux/list.h:351:20: note: in definition of macro ‘list_entry’
container_of(ptr, type, member)
^
ex1.c: In function ‘my_show’:
ex1.c:70:31: error: expected expression before ‘struct’
tmp = list_entry(pos, struct node, list);
^
/usr/src/linux-headers-3.11.0-12/include/linux/list.h:351:20: note: in definition of macro ‘list_entry’
container_of(ptr, type, member)
^
ex1.c: In function ‘my_delete_all’:
ex1.c:83:31: error: expected expression before ‘struct’
tmp = list_entry(pos, struct node, list);
^
/usr/src/linux-headers-3.11.0-12/include/linux/list.h:351:20: note: in definition of macro ‘list_entry’
container_of(ptr, type, member)
^
源代码:- #include <linux/list.h>
- #include <stdio.h>
- #include <malloc.h>
- struct node
- {
- int num;
- struct list_head list;
- };
- int my_insert(struct node *my_list, int num)
- {
- struct node *tmpNode;
- tmpNode = (struct node *)malloc(sizeof(struct node));
- tmpNode->num = num;
- INIT_LIST_HEAD(&tmpNode->list);
- list_add_tail(&tmpNode->list, &my_list->list);
- }
- int my_delete(struct node *my_list, int num)
- {
- struct node *tmp;
- struct list_head *pos, *n;
-
- list_for_each_safe(pos, n, &my_list->list)
- {
- tmp = list_entry(pos,struct node, list);
- if (tmp->num == num)
- list_del(pos);
- free(tmp);
- }
- }
- int my_init(struct node **my_listp)
- {
- *my_listp = (struct node *)malloc(sizeof(struct node));
- INIT_LIST_HEAD(&(*my_listp)->list);
- return 1;
- }
- int my_sum(struct node *my_list)
- {
- struct list_head *pos, *n;
- struct node *tmp;
- int sum = 0;
- list_for_each_safe(pos, n, &my_list->list)
- {
- tmp = list_entry(pos, struct node, list);
- sum += tmp->num;
- }
- return sum;
- }
- void my_show(struct node *my_list)
- {
- struct node *tmp;
- struct list_head *pos, *n;
- list_for_each_safe(pos, n, &my_list->list)
- {
- tmp = list_entry(pos, struct node, list);
- printf("num: %d\n", tmp->num);
- }
- }
- void my_delete_all(struct node *my_list)
- {
- struct node *tmp;
- struct list_head *pos, *n;
- list_for_each_safe(pos, n, &my_list->list)
- {
- list_del(pos);
- tmp = list_entry(pos, struct node, list);
- free(tmp);
- }
- }
- int main()
- {
- int i;
- struct node *my_list;
- my_init(&my_list);
- for (i = 0; i < 10; i++)
- my_insert(my_list, i+1);
- my_show(my_list);
- my_delete(my_list, 3);
- my_show(my_list);
- int sum = my_sum(my_list);
- printf("sum: %d", sum);
- my_insert(my_list, sum);
- my_show(my_list);
- my_delete_all(my_list);
- return 0;
- }
复制代码 |
|