Linux内核模块编译 最简单的hello world

2014-11-24 07:26:09 · 作者: · 浏览: 2

驱动源代码


可用,只是MODULE_LICENCE行老报错,加上#也不行,就注释掉了


后注:lincese拼错了,应该是MODULE_LICENSE


//hello world driver for linux 2.6
#include
#include
#include
//MODULE_LICENCE("GPL");
static int __init hello_init(void)
{
printk(KERN_ALERT "Hello, world! from kernel space....\n");
return 0;
}

static void __exit hello_exit(void)
{
printk(KERN_ALERT "Goodbye, world! Leaving kernel space....\n");
}
module_init(hello_init);
module_exit(hello_exit);


Makefile文件


obj-m += hello.o
all:
make -C /lib/modules/`uname -r`/build SUBDIRS=$(PWD) modules
clean:
make -C /lib/modules/`uname -r`/build SUBDIRS=$(PWD) clean


插入模块


#insmod hello.ko


查看模块插入情况


#lsmod | grep hello


#tail /var/log/messages


移除模块


#rmmod hello


#taile /var/log/messages