免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2448 | 回复: 4
打印 上一主题 下一主题

[内核入门] list.h 用户态下 list_entry错误 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-04-10 18:27 |只看该作者 |倒序浏览
本帖最后由 感性狂野理智不灭 于 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)
                    ^
源代码:
  1. #include <linux/list.h>
  2. #include <stdio.h>
  3. #include <malloc.h>


  4. struct node
  5. {
  6.     int num;
  7.     struct list_head list;
  8. };




  9. int my_insert(struct node *my_list, int num)
  10. {
  11.     struct node *tmpNode;

  12.     tmpNode = (struct node *)malloc(sizeof(struct node));
  13.     tmpNode->num = num;
  14.     INIT_LIST_HEAD(&tmpNode->list);
  15.     list_add_tail(&tmpNode->list, &my_list->list);
  16. }

  17. int my_delete(struct node *my_list, int num)
  18. {
  19.     struct node *tmp;
  20.     struct list_head *pos, *n;
  21.    

  22.     list_for_each_safe(pos, n, &my_list->list)
  23.     {
  24.         tmp = list_entry(pos,struct node, list);
  25.         if (tmp->num == num)
  26.             list_del(pos);
  27.         free(tmp);
  28.     }
  29. }

  30. int my_init(struct node **my_listp)
  31. {
  32.     *my_listp = (struct node *)malloc(sizeof(struct node));
  33.     INIT_LIST_HEAD(&(*my_listp)->list);

  34.     return 1;
  35. }

  36. int my_sum(struct node *my_list)
  37. {
  38.     struct list_head *pos, *n;
  39.     struct node *tmp;
  40.     int sum = 0;

  41.     list_for_each_safe(pos, n, &my_list->list)
  42.     {
  43.        tmp = list_entry(pos, struct node, list);
  44.         sum += tmp->num;
  45.     }

  46.     return sum;
  47. }

  48. void my_show(struct node *my_list)
  49. {
  50.     struct node *tmp;
  51.     struct list_head *pos, *n;

  52.     list_for_each_safe(pos, n, &my_list->list)
  53.     {
  54.        tmp = list_entry(pos, struct node, list);
  55.         printf("num: %d\n", tmp->num);
  56.     }
  57. }

  58. void my_delete_all(struct node *my_list)
  59. {
  60.     struct node *tmp;
  61.     struct list_head *pos, *n;

  62.     list_for_each_safe(pos, n, &my_list->list)
  63.     {
  64.         list_del(pos);
  65.         tmp = list_entry(pos, struct node, list);
  66.         free(tmp);
  67.     }
  68. }

  69. int main()
  70. {
  71.     int i;

  72.     struct node *my_list;
  73.     my_init(&my_list);

  74.     for (i = 0; i < 10; i++)
  75.         my_insert(my_list, i+1);

  76.     my_show(my_list);

  77.     my_delete(my_list, 3);
  78.     my_show(my_list);

  79.     int sum = my_sum(my_list);
  80.     printf("sum: %d", sum);

  81.     my_insert(my_list, sum);
  82.     my_show(my_list);

  83.     my_delete_all(my_list);

  84.     return 0;
  85. }
复制代码

论坛徽章:
0
2 [报告]
发表于 2014-04-10 19:28 |只看该作者
list_entry就是container_of,是用下面的编译试试看,以前测试过是可以用的.
-----------------------------------------------------------------------
#define container_of(ptr, type, member) ( { \
        const typeof( ((type *)0)->member ) *__mptr = (ptr); \
        (type *)( (char *)__mptr - offsetof(type,member) ); } )

#define list_entry(ptr, type, member) container_of(ptr, type, member)

论坛徽章:
0
3 [报告]
发表于 2014-04-14 17:26 |只看该作者
回复 2# 鬼鬼一哈


   代码加上这一段已通过编译,,   谢啦~~     代码还有错误, 继续改去

论坛徽章:
0
4 [报告]
发表于 2014-04-14 21:47 |只看该作者
回复 3# 感性狂野理智不灭


    ok   完工,

my_delete函数  free函数位置有问题

运行无误

  1. #include <linux/list.h>

  2. #include <stdio.h>

  3. #include <malloc.h>





  4. #define container_of(ptr, type, member) ( { \

  5.         const typeof( ((type *)0)->member ) *__mptr = (ptr); \

  6.         (type *)( (char *)__mptr - offsetof(type,member) ); } )



  7. #define list_entry(ptr, type, member) container_of(ptr, type, member)





  8. struct node

  9. {

  10.     int num;

  11.     struct list_head list;

  12. };









  13. int my_insert(struct node *my_list, int num)

  14. {

  15.     struct node *tmpNode;



  16.     tmpNode = (struct node *)malloc(sizeof(struct node));

  17.     tmpNode->num = num;

  18.     INIT_LIST_HEAD(&tmpNode->list);

  19.     list_add_tail(&tmpNode->list, &my_list->list);

  20. }



  21. int my_delete(struct node *my_list, int num)

  22. {

  23.     struct node *tmp;

  24.     struct list_head *pos, *n;

  25.    



  26.     list_for_each_safe(pos, n, &my_list->list)

  27.     {

  28.         tmp = list_entry(pos,struct node, list);

  29.         if (tmp->num == num)

  30.         {

  31.             list_del(pos);

  32.             free(tmp);

  33.             //break;

  34.         }

  35.     }

  36. }



  37. int my_init(struct node **my_listp)

  38. {

  39.     *my_listp = (struct node *)malloc(sizeof(struct node));

  40.     INIT_LIST_HEAD(&(*my_listp)->list);



  41.     return 1;

  42. }



  43. int my_sum(struct node *my_list)

  44. {

  45.     struct list_head *pos, *n;

  46.     struct node *tmp;

  47.     int sum = 0;



  48.     list_for_each_safe(pos, n, &my_list->list)

  49.     {

  50.         tmp = list_entry(pos, struct node, list);

  51.         sum += tmp->num;

  52.     }



  53.     return sum;

  54. }



  55. void my_show(struct node *my_list)

  56. {

  57.     struct node *tmp;

  58.     struct list_head *pos, *n;



  59.     list_for_each_safe(pos, n, &my_list->list)

  60.     {

  61.         tmp = list_entry(pos, struct node, list);

  62.         printf("num: %d\n", tmp->num);

  63.     }

  64.     printf("\n");

  65. }



  66. void my_delete_all(struct node *my_list)

  67. {

  68.     struct node *tmp;

  69.     struct list_head *pos, *n;



  70.     list_for_each_safe(pos, n, &my_list->list)

  71.     {

  72.         list_del(pos);

  73.         tmp = list_entry(pos, struct node, list);

  74.         free(tmp);

  75.     }

  76. }



  77. int main()

  78. {

  79.     int i;



  80.     struct node *my_list;

  81.     my_init(&my_list);



  82.     for (i = 0; i < 10; i++)

  83.         my_insert(my_list, i+1);



  84.     my_show(my_list);



  85.     my_delete(my_list, 3);

  86.     my_show(my_list);



  87.     int sum = my_sum(my_list);

  88.     printf("sum: %d\n\n", sum);



  89.     my_insert(my_list, sum);

  90.     my_show(my_list);



  91.     my_delete_all(my_list);



  92.     return 0;

  93. }
复制代码

论坛徽章:
0
5 [报告]
发表于 2014-12-27 12:22 |只看该作者
为什么加上2楼的代码就可以了呢?
2楼的代码在内核的list.h文件是有的啊
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP