linux載入驅動的兩種makefile檔案 載入驅動過程中,需要個Makefile檔案來完成編譯的命令。自己經常把把Makefile裡面的命令搞混,這次,專門的研究了一下Makefile檔案裡面的東東,特寫出來和大家分享一下 一 一個簡單的驅動程式例子: www.2cto.com [plain] /*====================================================================== A kernel module: book This example is to introduce module params The initial developer of the original code is Baohua Song <author@linuxdriver.cn>. All Rights Reserved. ======================================================================*/ #include <linux/init.h> #include <linux/module.h> MODULE_LICENSE("Dual BSD/GPL"); www.2cto.com static char *book_name = "dissecting Linux Device Driver"; static int num = 4000; static int book_init(void) { printk(KERN_INFO " book name:%s\n",book_name); printk(KERN_INFO " book num:%d\n",num); return 0; } static void book_exit(void) { printk(KERN_INFO " Book module exit\n "); } module_init(book_init); module_exit(book_exit); module_param(num, int, S_IRUGO); module_param(book_name, charp, S_IRUGO); MODULE_AUTHOR("Song Baohua, author@linuxdriver.cn"); MODULE_DESCRIPTION("A simple Module for testing module params"); MODULE_VERSION("V1.0"); 二 Makefile檔案有兩種寫法: 一種是: [html] # Add your debugging flag (or not) to CFLAGS ifneq ($(KERNELRELEASE),) obj-m := boot.o else KERNELDIR ?= /lib/modules/$(shell uname -r)/build PWD := $(shell pwd) default: $(MAKE) -C $(KERNELDIR) M=$(PWD) modules endif www.2cto.com 另外一種是:[plain] # Add your debugging flag (or not) to CFLAGS ifneq ($(KERNELRELEASE),) obj-m := boot.o else KERNELDIR ?= /usr/src/linux-headers-2.6.38-8-generic PWD := $(shell pwd) default: $(MAKE) -C $(KERNELDIR) M=$(PWD) modules endif 比較兩者可以發現,該兩個Makefile的唯一差別是KERNELDIR的不同