注:若是想要按照本篇實踐,須要有能運行的arm開發板和對應版本的內核(若是想在Linux主機上編譯運行,請參考文末附1)linux
#include <linux/init.h>#include <linux/module.h>static int hello_init(void) { printk(KERN_INFO "[init] Can you feel me?\n"); return 0; }static void hello_exit(void) { printk(KERN_INFO "[exit] Yes.\n"); } module_init(hello_init); module_exit(hello_exit); MODULE_AUTHOR("Alan Wang <alan@wrcode.com>"); MODULE_LICENSE("GPL"); MODULE_DESCRIPTION("A simple Hello World Module"); MODULE_ALIAS("A simple module");
/* Kconfig */ui
# drivers/alan_test/Kconfigmenu "ALAN_TEST Driver "comment "ALAN_TEST Driver comment"config ALAN_TEST bool "ALAN_TEST support"config HELLO tristate "hello module test" depends on ALAN_TEST endmenu
/* Makefile */spa
# drivers/alan_test/Makefile# makefile for the ALAN_TESTobj-$(CONFIG_HELLO) += hello.o
drivers 下的 Kconfig 末尾 endmenu 前添加一行code
source "drivers/alan_test/Kconfig"endmenu
drivers 下的 Makefile 末尾加一行orm
obj-$(CONFIG_ALAN_TEST) += alan_test/
依次是:Device Drivers —->
ALAN_TEST Driver —->
*** ALAN_TEST Driver comment ***
[*] ALAN_TEST support
< M > hello module testci
退出保存 menuconfig開發
使用 make 編譯
生成的 zImage (arch/arm/boot/zImage) 燒寫到開發板對應位置。
生成的 hello.ko(drivers/alan_test/hello.ko) 複製到開發板Linux系統的一個目錄下。get
把 hello.c 和 Makefile 放在同一目錄
/* Makefile */
KERNEL_VER = $(shell uname -r)# kernel modulesobj-m += hello.o# specify flags for the module compilationEXTRA_CFLAGS = -g -O0 build: kernel_moduleskernel_modules: make -C /lib/modules/$(KERNEL_VER)/build M=$(CURDIR) modulesclean: make -C /lib/modules/$(KERNEL_VER)/build M=$(CURDIR) clean