Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1021652
  • 博文数量: 297
  • 博客积分: 11721
  • 博客等级: 上将
  • 技术积分: 3431
  • 用 户 组: 普通用户
  • 注册时间: 2009-05-25 10:21
文章分类

全部博文(297)

文章存档

2016年(9)

2011年(71)

2010年(137)

2009年(80)

分类: LINUX

2010-05-27 23:43:29

1. 简单的Makefile编译hello.c模块分析
Makefile内容如下:
obj-m := hello.o

编译过程如下:
[root@localhost driver_study]# make -C /usr/src/kernels/2.6.31.5-127.fc12.i686.PAE/ M=$(pwd) modules
make: Entering directory `/usr/src/kernels/2.6.31.5-127.fc12.i686.PAE'
  Building modules, stage 2.
  MODPOST 1 modules
make: Leaving directory `/usr/src/kernels/2.6.31.5-127.fc12.i686.PAE'
[root@localhost driver_study]#

由此可见:
make 先执行 "-C /usr/src/kernels/2.6.31.5-127.fc12.i686.PAE/ M=$(pwd) modules" 之后到Makefile中找编译目标 "obj-m := hello.o"


2.复杂的Makefile编译hello.c模块分析
分析hello.c模块的Makefile


Makefile内容如下:
ifeq ($(KERNELRELEASE),)
    KERNELDIR ?= /lib/modules/$(shell uname -r)/build
    PWD := $(shell pwd)
modules:
        $(MAKE) -C $(KERNELDIR) M=$(PWD) modules

modules_install:
        $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install

clean:
        rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions

.PHONY: modules modules_install clean

else
    obj-m := hello.o
endif

执行make命令:
[root@localhost misc-modules]# make
make -C /lib/modules/2.6.31.5-127.fc12.i686.PAE/build M=/root/src/examples/misc-modules modules
make[1]: Entering directory `/usr/src/kernels/2.6.31.5-127.fc12.i686.PAE'
  CC [M]  /root/src/examples/misc-modules/hello.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /root/src/examples/misc-modules/hello.mod.o
  LD [M]  /root/src/examples/misc-modules/hello.ko
make[1]: Leaving directory `/usr/src/kernels/2.6.31.5-127.fc12.i686.PAE'

由于ifeq ($(KERNELRELEASE),) 条件成立,Makefile 定义 KERNELDIR 和 PWD
1) 执行默认编译选项(第一个目标modules): $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
2) 之后再次进入Makefile查找编译目标,由于前面以前定义了 KERNELDIR ,此时 ifeq ($(KERNELRELEASE),)条件为假,
所以执行
else
    obj-m := hello.o
从而查找到编译目标为 obj-m := hello.o
最终编译目标的流程也是:
1)执行 make -C /lib/modules/2.6.31.5-127.fc12.i686.PAE/build M=/root/src/examples/misc-modules modules
2)查找编译目标:obj-m := hello.o

hello.ko模块的全自动Makefile编写
ifeq ($(KERNELRELEASE),)
    KERNELDIR ?= /lib/modules/$(shell uname -r)/build
    PWD := $(shell pwd)

modules:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules

modules_install:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install

clean:
    rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions

.PHONY: modules modules_install clean

else
    obj-m := hello.o
endif


阅读(3174) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~