在Feodra Linux 14編譯驅動很是方便,咱們可使用社區提供好的Package,來進行編譯工做。linux
*編譯過程* shell
首先在系統中安裝kernel相關的package:api
yum -y install kernel-devel kernel-headers安裝成功後yum會返回日誌相似以下:
Installed: kernel-devel.i686 0:2.6.35.10-74.fc14 Updated: kernel-headers.i686 0:2.6.35.10-74.fc14Fedora 14會保證內核版本與kernel開發庫的代碼版本一致,若是須要確認,能夠試試看用命令來確認:
uname -a執行上述命令應該返回結果相似以下:
Linux fedora14 2.6.35.6-45.fc14.i686 #1 SMP Mon Oct 18 23:56:17 UTC 2010 i686 i686 i386 GNU/Linux與安裝的kernel-devel及kernel-headers版本一致便可。 接下來在同一目錄下建立兩個文件,一個是helloworld.c
#include /* Needed by all modules */ #include /* Needed for KERN_INFO */ #include /* Needed for the macros */ static int __init hello_start(void) { printk(KERN_INFO "Loading hello module...\n"); printk(KERN_INFO "Hello world\n"); return 0; } static void __exit hello_end(void) { printk(KERN_INFO "Goodbye Mr.\n"); } module_init(hello_start); module_exit(hello_end);另外一個爲Makefile:
# Comment/uncomment the following line to disable/enable debugging #DEBUG = y # Add your debugging flag (or not) to CFLAGS ifeq ($(DEBUG),y) DEBFLAGS = -O -g # "-O" is needed to expand inlines else DEBFLAGS = -O2 endif EXTRA_CFLAGS += $(DEBFLAGS) #-I$(LDDINCDIR) ifneq ($(KERNELRELEASE),) # call from kernel build system obj-m := helloworld.o else KERNELDIR ?= /lib/modules/$(shell uname -r)/build PWD := $(shell pwd) default: $(MAKE) -C $(KERNELDIR) M=$(PWD) modules #LDDINCDIR=$(PWD)/../include modules endif clean: rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions depend .depend dep: $(CC) $(CFLAGS) -M *.c > .depend ifeq (.depend,$(wildcard .depend)) include .depend endif建立完成後,對代碼進行編輯,在上述代碼所在目錄執行命令以下:
make編譯過程相似以下:
make -C /lib/modules/2.6.35.6-45.fc14.i686/build M=/home/liweinan/projs modules #LDDINCDIR=/home/liweinan/projs/../include modules make[1]: Entering directory `/usr/src/kernels/2.6.35.6-45.fc14.i686' CC [M] /home/liweinan/projs/helloworld.o Building modules, stage 2. MODPOST 1 modules CC /home/liweinan/projs/helloworld.mod.o LD [M] /home/liweinan/projs/helloworld.ko make[1]: Leaving directory `/usr/src/kernels/2.6.35.6-45.fc14.i686'helloworld.ko就是咱們要的module,安裝它試試看:
insmod helloworld.ko若是安裝正確則不會有消息返回。看看dmesg裏面的日誌:
dmesg | tail應該有下述日誌:
[ 1138.690913] helloworld: module license 'unspecified' taints kernel. [ 1138.690915] Disabling lock debugging due to kernel taint [ 1138.691012] Loading hello module... [ 1138.691014] Hello world
說明安裝成功。 bash
*常見問題*網站
-1 Invalid module format
這個是在安裝模塊時可能會遇到的問題,通常是因爲使用的kernel library與kernel版本不一致形成,在Fedora 14下經過yum安裝,應該會由系統保證環境的一致性,但若是你真的遇到了這個問題,那麼可能就須要本身來編譯Linux Kernel,Fedora針對內核編譯也有十分方便的方法,請參考這篇文檔 http://fedoraproject.org/wiki/Docs/CustomKernel ui
*關於Linux內核及驅動開發的一些有用資源* google
文中所用代碼來源在這裏: http://www.linuxquestions.org/questions/programming-9/trying-to-compile-hello-world-kernel-module-please-help-439353/ .net
http://kernelnewbies.org/ - 面向內核開發新手的網站,很多有用資源 debug
http://lwn.net/Articles/2.6-kernel-api/ - linux 2.6內核的新版本特性,很是有用,Linux內核每一次微小升級作出的改變,都須要隨時瞭解。 日誌
http://lwn.net/Kernel/LDD3/ - Linux驅動開發第三版,注意這本書裏面的很多代碼在最新的2.6內核已經不可用了。好比在書中的樣例代碼中常常見到的linux/config.h在最新的2.6內核版本中已經再也不存在。遇到問題時多多利用google能夠解決很多問題。