简单linux 2.6内核驱动模块编程
简单示例如下:
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
int init_module(void)
{
printk("hello kernel/n");
return 0;
}
void cleanup_module()
{
printk("Bye kernel/n");
}
或 (在2.4内核中的写法)
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
static int __init hello_init(void)
{
printk("hello kernel/n");
return 0;
}
static void __exit hello_exit()
{
printk("Bye kernel/n");
}
module_init(hello_init);
module_exit(hello_exit);
最主要的是编写Makefile 其内容如下:
obj-m := hello.o
PWD := $(shell pwd)
KVER := $(shell uname -r)
KDIR := /lib/modules/$(KVER)/build
all:
$(MAKE) -C $(KDIR) M=$(PWD) 注:-C一定要大写
clean:
rm -rf *.o *.cmd *.mod.c *.ko *.markers *.symvers
在命令行下执行make即可生成hello.ko文件,然后insmod hello.ko将该模块加载到内核中
用lsmod | grep hello命令查看是否加载成功
用dmesg命令查看输出的内容或者用tail /var/log/messages 查看
用rmmod hello命令将该模块卸载