【问题标题】:error: assignment from incompatible pointer type [-Werror=incompatible-pointer-types]错误:来自不兼容指针类型的赋值 [-Werror=incompatible-pointer-types]
【发布时间】:2019-03-21 22:25:57
【问题描述】:

我正在开发一个 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】:

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

    在最新的内核(自 4.15 起)中,此函数字段使用 struct timer_list * 参数而不是 unsigned long 声明,如下所示:

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

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

    【讨论】:

    • 是的,我只是注意到它们不同。我检查了一个比我正在使用的当前内核更新的内核。我的错;)
    猜你喜欢
    • 2021-11-16
    • 1970-01-01
    • 1970-01-01
    • 2019-06-09
    • 2019-08-09
    • 2017-10-24
    • 1970-01-01
    • 2014-12-14
    • 2019-06-05
    相关资源
    最近更新 更多