关于警告:initialization from incompatible pointer type [enabled by default]
2013-08-27 14:22
337 查看
加了个sys文件系统方便调试,但是出现了如下编译警告,感觉很不爽,让我折腾了两个小时,终于搞定。
/home/apuser/mywork/4.1-3.4/kernel/drivers/input/misc/headset_xxx.c:800:9: warning: initialization from incompatible pointer type [enabled by default]
/home/apuser/mywork/4.1-3.4/kernel/drivers/input/misc/headset_xxxx.c:800:9: warning: (near initialization for 'headset_suspend_attr.store') [enabled by default]
原因:
函数参数类型不匹配。
出现警告时的函数类型定义:
分析参考:
struct kobj_attribute {
struct attribute attr;
ssize_t (*show)(struct kobject *kobj, struct kobj_attribute *attr,
char *buf);
ssize_t (*store)(struct kobject *kobj, struct kobj_attribute *attr,
const char *buf, size_t count);
};
总结,还是太大意了。。。。。
我的代码参考:
#ifdef HEADSET_DBG
/***create sys fs for debug***/
static int suspend_sts = 0;
static int headset_suspend(struct platform_device *dev, pm_message_t state);
static int headset_resume(struct platform_device *dev);
static ssize_t headset_suspend_show(struct kobject *kobj, struct kobj_attribute *attr, char *buff)
{
PRINT_INFO("headset_suspend_show\n");
return sprintf(buff, "%d\n", suspend_sts);
}
static ssize_t headset_suspend_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buff, size_t len)
{
pm_message_t pm_message_t_temp = {
.event = 0,
};
unsigned long suspend = simple_strtoul(buff, NULL, 10);
PRINT_INFO("headset_suspend_store. buff = %s set_val = %ld\n", buff, suspend);
if(suspend) {
headset_suspend(NULL, pm_message_t_temp);
}
else {
headset_resume(NULL);
}
return len;
}
static struct kobject *headset_suspend_kobj = NULL;
static struct kobj_attribute headset_suspend_attr =
__ATTR(suspend, 0644, headset_suspend_show, headset_suspend_store);
static int headset_suspend_sysfs_init(void)
{
int ret = -1;
headset_suspend_kobj = kobject_create_and_add("headset", kernel_kobj);
if (headset_suspend_kobj == NULL) {
ret = -ENOMEM;
PRINT_ERR("register sysfs failed. ret = %d\n", ret);
return ret;
}
ret = sysfs_create_file(headset_suspend_kobj, &headset_suspend_attr.attr);
if (ret) {
PRINT_ERR("create sysfs failed. ret = %d\n", ret);
return ret;
}
PRINT_INFO("headset_suspend_sysfs_init success\n");
return ret;
}
/***create sys fs for debug***/
#endif
/home/apuser/mywork/4.1-3.4/kernel/drivers/input/misc/headset_xxx.c:800:9: warning: initialization from incompatible pointer type [enabled by default]
/home/apuser/mywork/4.1-3.4/kernel/drivers/input/misc/headset_xxxx.c:800:9: warning: (near initialization for 'headset_suspend_attr.store') [enabled by default]
原因:
函数参数类型不匹配。
出现警告时的函数类型定义:
static ssize_t headset_suspend_store(struct kobject *kobj, struct kobj_attribute *attr, char *buff, size_t len)
修复后的定义:
static ssize_t headset_suspend_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buff, size_t len)就是把参数3的类型没有用const限定所致
分析参考:
struct kobj_attribute {
struct attribute attr;
ssize_t (*show)(struct kobject *kobj, struct kobj_attribute *attr,
char *buf);
ssize_t (*store)(struct kobject *kobj, struct kobj_attribute *attr,
const char *buf, size_t count);
};
总结,还是太大意了。。。。。
我的代码参考:
#ifdef HEADSET_DBG
/***create sys fs for debug***/
static int suspend_sts = 0;
static int headset_suspend(struct platform_device *dev, pm_message_t state);
static int headset_resume(struct platform_device *dev);
static ssize_t headset_suspend_show(struct kobject *kobj, struct kobj_attribute *attr, char *buff)
{
PRINT_INFO("headset_suspend_show\n");
return sprintf(buff, "%d\n", suspend_sts);
}
static ssize_t headset_suspend_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buff, size_t len)
{
pm_message_t pm_message_t_temp = {
.event = 0,
};
unsigned long suspend = simple_strtoul(buff, NULL, 10);
PRINT_INFO("headset_suspend_store. buff = %s set_val = %ld\n", buff, suspend);
if(suspend) {
headset_suspend(NULL, pm_message_t_temp);
}
else {
headset_resume(NULL);
}
return len;
}
static struct kobject *headset_suspend_kobj = NULL;
static struct kobj_attribute headset_suspend_attr =
__ATTR(suspend, 0644, headset_suspend_show, headset_suspend_store);
static int headset_suspend_sysfs_init(void)
{
int ret = -1;
headset_suspend_kobj = kobject_create_and_add("headset", kernel_kobj);
if (headset_suspend_kobj == NULL) {
ret = -ENOMEM;
PRINT_ERR("register sysfs failed. ret = %d\n", ret);
return ret;
}
ret = sysfs_create_file(headset_suspend_kobj, &headset_suspend_attr.attr);
if (ret) {
PRINT_ERR("create sysfs failed. ret = %d\n", ret);
return ret;
}
PRINT_INFO("headset_suspend_sysfs_init success\n");
return ret;
}
/***create sys fs for debug***/
#endif
相关文章推荐
- warning: assignment from incompatible pointer type [enabled by default]
- 关于问题warning: passing arg 2 of `bind' from incompatible pointer type理解
- container_of 报出 warning: initialization from incompatible pointer type 解决办法
- warning: initialization from incompatible pointer type 分析
- Flex: yylval warning: assignment makes pointer from integer without a cast [enabled by default]
- initialization from incompatible pointer type
- warning: initialization from incompatible pointer type
- 出现警告:incompatible pointer types returning from a function with result type
- warning:initialization discards ‘const’ qualifier from pointer target type 解决方法
- 一般c++编译出现"name lookup of '变量名' changed [enabled by default]"的警告
- assignment from incompatible pointer type
- warning : incompatible implicit declaration of built-in function 'exit' [enabled by default]
- 警告⚠️Assigning to 'id<LoginViewDelegate>' from incompatible type 'LoginViewController *__strong'
- Assigning to "id<CALayerDelegate> _Nullable" from incompatible type "ZXCapture *const __strong" 的警告提示信息
- initialization discards 'const' qualifier from pointer target type
- Xcode编译警告Assigning to 'id<XXXDelegat> ——Nullable' from incompatible type 'XXXView *const_strong'
- warning: passing argument 2 of 'request_irq' from incompatible pointer type导致的中断申请失败和
- warning: assignment from incompatible pointer type (注册hook时出现了)
- 关于如何解决warning: assignment makes pointer from integer without a cast的警告
- warning: incompatible implicit declaration of built-in function sqrt [enabled by default]