繁体   English   中英

Linux内核链接列表和“ typeof”错误

[英]Linux Kernel Linked List and 'typeof' error

我正在尝试在Linux内核中实现链接列表。

我认为我的头文件很好,但是当我尝试编译主文件以测试我所做的事情时,我并不真正了解出什么问题了...

我的文件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,typeof(*cur), member))

#endif /* _LIST_H */

我的主文件:

#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= %d\n", 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, typeof(*cur),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, &mylist.list, list)
  ^
list.h:36:39: error: expected expression before ‘typeof’
   cur = container_of(cur->member.next,typeof(*cur), member))
                                       ^
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, &mylist.list, list)
  ^

似乎我没有以正确的方式操纵'typeof',但我不明白为什么...如果有人可以解释什么地方出了问题,谢谢:)

您的错误是由于缺少offsetof而不是内置运算符(如sizeof )引起的, 而是由 stddef.h定义的真实宏引起的

如果没有包含此文件,编译器会假定它必须引用一个函数,这将导致编译器完全误解语法,并给您一个不清楚的错误消息(因为函数不能接受类型作为其参数)。

您可以通过包含stddef.h来修复即时语法错误。 通过编译并启用尽可能多的严格开关和警告(至少使用-Wall ),您可以洞悉这些情况下出了什么问题。 默认情况下,GCC以完全宽松的模式进行编译,除其他事项外,GCC并不将隐式函数声明视为错误,因此,此问题的真正根本原因已被编译器完全忽略。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM