Linux Kernel 学习笔记1:module的基本结构和编译

来源:互联网 发布:mmd 动作数据 r-18 编辑:程序博客网 时间:2024/06/18 14:17

(本章基于:linux-4.4.0-37)

Linux module基本结构如下:

init、exit函数,init、exit函数加载、GPL和作者声明


helloworld.c

#include <linux/init.h>#include <linux/module.h>static __init int hello_init(void){        printk(KERN_ALERT "helloworld!\n");        return 0;}static __exit void hello_exit(void){        printk(KERN_ALERT "helloworld exit!\n");}module_init(hello_init);module_exit(hello_exit);MODULE_LICENSE("GPL");MODULE_AUTHOR("Stone");


Makefile:

CONFIG_MODULE_SIG=nobj-m:=hello.ohello-objs:=helloworld.oKERN_DIR=/lib/modules/`uname -r`/build/PWD_DIR=$(shell pwd)default:        $(MAKE) -C $(KERN_DIR) M=$(PWD_DIR) modulesclean:        rm -rf *.o *.ko *.mod.c .*.cmd .*.cmd.c modules.* Module.* .tmp_versions

hello为模块名,编译结果为hello.ko

(备注: 添加 ONFIG_MODULE_SIG=n,避免出现如下错误信息:

helloworld: module verification failed: signature and/or required key missing - tainting kernel





0 0
原创粉丝点击