首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計綜合問答

Linux 驅動編譯 initialization from incompatible pointer type:結構體函數指針類型不匹配

     編輯:程式設計綜合問答

錯誤位置

static struct class_attribute hcsr04_class_attrs[] = {
    __ATTR(value, S_IRUGO | S_IWUSR, hcsr04_value_read, hcsr04_value_write),
    __ATTR_NULL,
};

原因

`__ATTR` 宏展開為 `struct attribute` 初始化,**第三/四參數需為 `ssize_t (*show)(struct class *, struct class_attribute *, char *)` 和 `ssize_t (*store)(struct class *, struct class_attribute *, const char *, size_t)` 類型**。若你的 `hcsr04_value_read/write` 簽名不匹配 (如參數順序錯、缺少 `size_t`、返回類型錯),即報此錯。

修正模板

static ssize_t hcsr04_value_show(struct class *c, struct class_attribute *attr, char *buf) {
    return sprintf(buf, "%d\n", current_value);
}
static ssize_t hcsr04_value_store(struct class *c, struct class_attribute *attr, const char *buf, size_t count) {
    kstrtoint(buf, 10, ¤t_value);
    return count;
}

排查步驟

  1. 對照內核源碼 `include/linux/sysfs.h` 中 `__ATTR` 定義確認函數指針類型。
  2. 編譯器報錯行號指向 `__ATTR` 行,實際錯在前面函數定義。
  3. 使用 `scripts/checkpatch.pl` 檢查代碼風格與類型。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved