linux内核模块安装hello

来源:互联网 发布:域名可以一年一年续费 编辑:程序博客网 时间:2024/05/29 11:16

首先编写内核模块源码

#include<linux/init.h>#include<linux/module.h>MODULE_LICENSE("GPL");static int hello_init(void){        printk("<0>hello brother\n");                //<0>表示打印的优先级,一共有8个好像        return 0;}static void hello_exit(void){        printk("<0>good bye\n");}module_init(hello_init);module_exit(hello_exit);

然后编写makefile

ifneq ($(KERNELRELEASE),)obj-m:=hello.o                                     //obj-m表示将该文件作为模块编译elseKDIR:=/lib/modules/2.6.18-194.el5/buildall:        make -C $(KDIR) M=$(PWD) modulesclean:        rm -f *.ko *.o *.mod.o *.mod.c *.symversendif
然后再执行make编译hello.c

最后执行insmod ./hello.ko                                 //insmod要在root权限下执行,因为insmod在/sbin/目录下,只有root用户才有的环境变量

rmmod hello


//-------------------------------------------------------------------------------------------------------------------------------------

多文件

首先编写内核模块源码

#include<linux/init.h>#include<linux/module.h>MODULE_LICENSE("GPL");extern int add(int a,int b);static int hello_init(void){int i=0;        printk("<0>hello brother\n");                //<0>表示打印的优先级,一共有8个好像i=add(4,5);printk(KERN_EMERG"%d",i);        return 0;}static void hello_exit(void){        printk("<0>good bye\n");}module_init(hello_init);module_exit(hello_exit);

add函数

int add(int a,int b){return a+b;}


然后编写makefile

ifneq ($(KERNELRELEASE),)obj-m:=hp.o                 //hp.o模块hp-objs:=hello.o add.o      //hp模块所依赖的文件elseKDIR:=/lib/modules/2.6.18-194.el5/buildall:        make -C $(KDIR) M=$(PWD) modulesclean:        rm -f *.ko *.o *.mod.o *.mod.c *.symversendif



0 0
原创粉丝点击