错误:从不兼容的指针类型分配[-Werror = incompatible-pointer-types]

问题描述 投票:0回答:1

我正在研究一个linux内核模块。

struct tcpsp_conn在头文件中定义如下:

struct tcpsp_conn {
...
struct timer_list timer; /* exp. timer*/
...
};

然后我声明一个指向结构的指针并尝试分配函数:

struct tcpsp_conn *cp;
cp->timer.function = tcpsp_conn_expire;

tcpsp_conn_expire函数的定义方式与内核的struct timer_list相同:

static void tcpsp_conn_expire(unsigned long data)

我不明白为什么我会收到此错误:错误:从不兼容的指针类型分配[-Werror = incompatible-pointer-types] cp-> timer.function = tcpsp_conn_expire;

它看起来没有类型问题。

linux-kernel kernel-module
1个回答
1
投票

你的tcpsp_conn_expire函数的类型不同于.function结构的timer_list字段的类型。

在最新的内核中(从4.15开始),这个函数字段用struct timer_list *参数而不是unsigned long声明,如下所示:

struct timer_list {
    ...
    void            (*function)(struct timer_list *);
    ...
};

有了这样的参数,你可以使用宏struct tcpsp_conn获得指向嵌入计时器的container_of结构的指针。

© www.soinside.com 2019 - 2024. All rights reserved.