linux加载驱动的两种makefile文件

本文详细解析了Makefile文件在驱动程序开发中的作用,通过一个具体实例展示了如何使用Makefile进行编译操作,并对比了两种常见的Makefile写法,旨在帮助开发者更好地理解和掌握Makefile的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 加载驱动过程中,需要个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的不同,


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值