error: assignment from incompatible pointer type [-Werror=incompatible-pointer-types]
error: assignment from incompatible pointer type [-Werror=incompatible-pointer-types]
我正在研究 linux 内核模块。
一个结构体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;
看起来类型没有问题。
您的 tcpsp_conn_expire
函数的类型 不同于 与 timer_list
结构的 .function
字段的类型。
在最新的内核中(自 4.15 起)此函数字段使用 struct timer_list *
参数而不是 unsigned long
声明,如下所示:
struct timer_list {
...
void (*function)(struct timer_list *);
...
};
有了这样的参数,你可以得到指向struct tcpsp_conn
结构的指针,定时器被嵌入其中,宏container_of
。
我正在研究 linux 内核模块。
一个结构体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;
看起来类型没有问题。
您的 tcpsp_conn_expire
函数的类型 不同于 与 timer_list
结构的 .function
字段的类型。
在最新的内核中(自 4.15 起)此函数字段使用 struct timer_list *
参数而不是 unsigned long
声明,如下所示:
struct timer_list {
...
void (*function)(struct timer_list *);
...
};
有了这样的参数,你可以得到指向struct tcpsp_conn
结构的指针,定时器被嵌入其中,宏container_of
。