hello程式
#include <linux/init.h>#include <linux/module.h>MODULE_LICENSE("Dual BSD/GPL");MODULE_AUTHOR("Hanson He");static int __init hello_init(void){ printk(KERN_ALERT "Hello world\n"); return 0;}static void __exit hello_exit(void){ printk(KERN_ALERT " Hello world exit\n");}module_init(hello_init);module_exit(hello_exit);
makefile內容
ifeq ($(KERNELRELEASE),)KERNELDIR ?=/root/Desktop/linux-2.6.32.2PWD := $(shell pwd)modules: $(MAKE) -C $(KERNELDIR) M=$(PWD) modulesmodules_install: $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_installclean: rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions.PHONY: modules modules_install cleanelse obj-m := hello.oendif
終端執行make出現如下的錯誤:
ERROR: Kernel configuration is invalid.include/linux/autoconf.h or include/config/auto.conf are missing. Run 'make oldconfig && make prepare' on kernel src to fix it.
原因是因為沒有編譯核心,解決方案如下編譯核心的步驟:
1. cp menu_mini2440_x35 .config
2.修改核心目錄下的Makefile
ARCH ?=arm
CROSS_COMPILE ?=arm-linux-
3.make menuconfig
4.make
做完這幾步之後在hello目錄下執行make即可出現hello.ko檔案
將hello.ko檔案拷貝到開發板執行
[root@FriendlyARM share]# insmod hello.ko Hello world[root@FriendlyARM share]# lsmodhello 505 0 - Live 0xbf000000[root@FriendlyARM share]# rmmod hello Hello world exitrmmod: module 'hello' not found
對於裝置,載入完驅動之後要建立節點檔案
mknod /dev/beep c major 0
beep 為裝置名稱
c 代表位字元裝置
major 代表為主裝置號
0 代表為次裝置號
帶參數的執行結果如下:
[root@FriendlyARM share]# insmod hello_ext.ko howmany=4 whom="jiejie"Hello jiejieHello jiejieHello jiejieHello jiejie[root@FriendlyARM share]# rmmod hello_ext.ko [root@FriendlyARM share]# rmmod hello_ext Hello world exitrmmod: module 'hello_ext' not found