代码如下:不过在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$