hello_drv.c
/
#include <linux/module.h>
#include <linux/init.h>
static __init hello_init(void)
{
printk("-------%s------\n",__FUNCTION__);
printk("--%s----------\n","hello world");
return 0;
}
static __exit hello_exit(void)
{
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");
//end
Makefile:
KERNEL_DIR = /home/linux/linux-3.14-fs4412//内核路径
DUR_DIR = $(shell pwd) //当前路径
ROOTS_DIR = /nfs/rootfs/dev_my ///.ko文件要存放的路径
/*
起初KERNELRELEASE为空,通过make -c 通过内核路径去调用内核中的makefile,并给KERNELRELEASE附上相应的版本值,将M路径下的文件编译为.KO文件,由于不知道编译那个文件,内核的makefile调用DUR_DIR路径下的makefile,这时KERNELRELEASE不再为空,则使用“obj-m +=目标文件.o”来编译生成.ko文件
*/
ifeq ($(KERNELRELEASE),)
all:
make -C $(KERNEL_DIR) M=$(DUR_DIR) modules
clean:
make -C $(KERNEL_DIR) M=$(DUR_DIR) clean
rm -r $(ROOTS_DIR)/*.ko
install:
cp *.ko $(ROOTS_DIR)
else
obj-m +=hello_drv.o
endif
/
现象ok:
[root@farsight dev_my]# [root@farsight dev_my]# insmod hello_drv.ko
[ 368.900000] -------hello_init------
[ 368.900000] --hello world----------