设备驱动模型 kobject示例及其调试

代码如下:不过在make不成功

#include <linux/device.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/string.h>
#include <linux/sysfs.h>
#include <linux/stat.h>

void kobject_test_release(struct kobject *kobject);

ssize_t kobject_test_show(struct kobject *kobject,struct attribute *attr,char *buf);
ssize_t kobject_test_store(struct kobject *kobject,struct attribute *attr,const char *buf,size_t count);

struct attribute test_attr ={
	.name ="kobject_test",
	.mode =S_IRWXUGO,
};
static struct attribute *def_attrs[] ={
	&test_attr,
	NULL,
};
struct sysfs_ops obj_test_sysops ={
	.show =kobject_test_show,
	.store =kobject_test_store,
};

void kobject_test_release(struct kobject *kobject)
{
	printk("kobject_test:kobject_test_releast(_).\n");
}

struct kobj_type ktype ={
	.release =kobject_test_release,
	.sysfs_ops =&obj_test_sysops,
	.default_attrs =def_attrs[],
};
ssize_t kobject_test_show(struct kobject *kobject,struct attribute *attr,char *buf)
{
	printk("call kobject_test_show().\n");
	printk("arrrname:%s.\n",attr->name);
	sprintf(buf,"%s\n",attr->name);
	return strlen(attr->name)+2;
}
ssize_t kobject_test_store(struct kobject *kobject,struct attribute *attr,const *buf,size_t)
{
	printk("call kobject_test_store()\n");
	printk("write:%s\n",buf);
	strcpy(attr->name,buf);
	return count;
}
struct kobject kobj;
static int kobject_test_init()
{
	printk("kobject test_init()\n");
	kobject_init_and_add(&kobj,&ktype,NULL,"kobject_test");
	return 0;
}
static int kobject_test_exit()
{
	printk("kobject test exit.\n");
	kobject_del(&kobj);
	return 0;
}

module_init(kobject_test_init);
module_exit(kobject_test_exit);
MODULE_AUTHOR("ABC");
MUDULE_LICENSE("Dual BSD/GPL");

错误如下:

cup@cup:~/driver/kobject$ sudo make

make -C /lib/modules/3.2.19/build M=/home/cup/driver/kobject modules

make[1]: Entering directory `/usr/src/linux-source-3.2.0'
  CC [M]  /home/cup/driver/kobject/kobject_test.o
/home/cup/driver/kobject/kobject_test.c:35:28: error: expected expression before ‘]’ token
/home/cup/driver/kobject/kobject_test.c:44:82: warning: type defaults to ‘int’ in declaration of ‘buf’ [-Wimplicit-int]
/home/cup/driver/kobject/kobject_test.c:44:9: error: conflicting types for ‘kobject_test_store’
/home/cup/driver/kobject/kobject_test.c:12:9: note: previous declaration of ‘kobject_test_store’ was here
/home/cup/driver/kobject/kobject_test.c: In function ‘kobject_test_store’:
/home/cup/driver/kobject/kobject_test.c:44:59: error: parameter name omitted
/home/cup/driver/kobject/kobject_test.c:47:2: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘const int *’ [-Wformat]
/home/cup/driver/kobject/kobject_test.c:48:2: warning: passing argument 1 of ‘strcpy’ discards ‘const’ qualifier from pointer target type [enabled by default]
/usr/src/linux-source-3.2.0/arch/x86/include/asm/string_32.h:9:14: note: expected ‘char *’ but argument is of type ‘const char *’
/home/cup/driver/kobject/kobject_test.c:48:2: warning: passing argument 2 of ‘strcpy’ from incompatible pointer type [enabled by default]
/usr/src/linux-source-3.2.0/arch/x86/include/asm/string_32.h:9:14: note: expected ‘const char *’ but argument is of type ‘const int *’
/home/cup/driver/kobject/kobject_test.c:49:9: error: ‘count’ undeclared (first use in this function)
/home/cup/driver/kobject/kobject_test.c:49:9: note: each undeclared identifier is reported only once for each function it appears in
/home/cup/driver/kobject/kobject_test.c: At top level:
/home/cup/driver/kobject/kobject_test.c:52:12: warning: function declaration isn’t a prototype [-Wstrict-prototypes]
/home/cup/driver/kobject/kobject_test.c:58:12: warning: function declaration isn’t a prototype [-Wstrict-prototypes]
/home/cup/driver/kobject/kobject_test.c: In function ‘__exittest’:
/home/cup/driver/kobject/kobject_test.c:66:1: warning: return from incompatible pointer type [enabled by default]
/home/cup/driver/kobject/kobject_test.c: At top level:
/home/cup/driver/kobject/kobject_test.c:68:16: error: expected declaration specifiers or ‘...’ before string constant
/home/cup/driver/kobject/kobject_test.c: In function ‘kobject_test_store’:
/home/cup/driver/kobject/kobject_test.c:50:1: warning: control reaches end of non-void function [-Wreturn-type]
make[2]: *** [/home/cup/driver/kobject/kobject_test.o] Error 1
make[1]: *** [_module_/home/cup/driver/kobject] Error 2
make[1]: Leaving directory `/usr/src/linux-source-3.2.0'
make: *** [modules] Error 2
cup@cup:~/driver/kobject$ 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值