doctorx第3季百度云:模块驱动 Makefile 模板
来源:百度文库 编辑:偶看新闻 时间:2024/05/03 18:22:32
模块驱动 Makefile 模板
(2010-06-22 12:02:56)转载▼TARGET=test_demo
#CROSS_COMPILE=arm-linux-
CC=$(CROSS_COMPILE)gcc
STRIP=$(CROSS_COMPILE)strip
#CFLAGS=-O2
ifeq($(KERNELRELEASE),)
all:
$(TARGET):
modules:
#-C $(KERNELDIR)指明跳转到内核源码目录下读取那里的Makefile
#M=$(PWD) 表明然后返回到当前目录继续读入、执行当前的Makefile
modules_install:
clean:
.PHONY:modules modules_installclean
#当从内核源码目录返回时,第二次进入Makefile时KERNELRELEASE已被被定义,所以可以执行以下的else语句
else
endif
另一种是倒过来的:
ifneq($(KERNELRELEASE),)
obj-m:=hello.o
else
KERNELDIR:=/lib/modules/$(shell uname -r)/build
PWD:=$(shell pwd)
default:
clean:
endif
看了LDD-3也是上面一种,复制过来看一下
# If KERNELRELEASE is defined, we've been invoked from the
# kernel build system and can use its language.
ifneq ($(KERNELRELEASE),)
obj-m := hello.o
# Otherwise we were called directly from the command
# line; invoke the kernel build system.
else
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
endif
Once again, we are seeing the extended GNU makesyntax in action. This makefile is read twice on a typical build.When the makefile is invoked from the command line, it notices thatthe KERNELRELEASE variable has not been set. It locatesthe kernel source directory by taking advantage of the fact thatthe symbolic link build in the installed modules directorypoints back at the kernel build tree. If you are not actuallyrunning the kernel that you are building for, you can supply aKERNELDIR= option on the command line, set theKERNELDIR environment variable, or rewrite the line thatsets KERNELDIR in the makefile. Once the kernel sourcetree has been found, the makefile invokes the default:target, which runs a second make command(parameterized in the makefile as $(MAKE)) to invoke thekernel build system as described previously. On the second reading,the makefile sets obj-m, and the kernel makefiles takecare of actually building the module.
This mechanism for building modules may strike you as a bitunwieldy and obscure. Once you get used to it, however, you willlikely appreciate the capabilities that have been programmed intothe kernel build system. Do note that the above is not a completemakefile; a real makefile includes the usual sort of targets forcleaning up unneeded files, installing modules, etc. See the makefiles in theexample source directory for a complete example.
总结:
Makefiel要做两件事:
1,如果内核没有被编译,先编译内核。
2,然后编译内核完后,编译模块。
就这么简单。。。。
分享 分享到新浪Qing0
顶
阅读(157)┊ 评论 (0)┊ 收藏(0) ┊转载(0) ┊ 顶▼ ┊打印┊举报