linux加载驱动的两种makefile文件

时间:2022-08-11 04:48:45

 加载驱动过程中,需要个Makefile文件来完成编译的命令。自己经常把把Makefile里面的命令搞混,这次,专门的研究了一下Makefile文件里面的东东,特写出来和大家分享一下

一 一个简单的驱动程序例子:

  

/*======================================================================
    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");                                
                                
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文件有两种写法:

  一种是:

# 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

另外一种是:

# 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的不同,