模板一
#To build modules outside of the kernel tree, we run "make"
#in the kernel source tree; the Makefile these then includes this
#Makefile once again.
#This conditional selects whether we are being included from the
#kernel Makefile or not.
ifeq ($(KERNELRELEASE),) #判断是否被上层makefile包含
# Assume the source tree is where the running kernel was built
# You should set KERNELDIR in the environment if it's elsewhere
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
# The current directory is passed to sub-makes as argument
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
# called from kernel build system: just declare what our modules are
obj-m := hello.o
endif
模板二
ifneq ($(KERNELRELEASE),)
module-objs:=hello.o #当有多个源文件时可以写成 hello1.o hello2.o
obj-m:=hello.o #有多个源文件时这个名字不能和上面的重复
else
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
endif
clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
.PHONY: modules modules_install clean
MAKEFILE基本知识
赋值符号 “=” “:=” “?=” “+=”
= 是最基本的赋值
:= 是覆盖之前的值
?= 是如果没有被赋值过就赋予等号后面的值
+= 是添加等号后面的值 两个链接符之间用自动添加空格
关于 := 和 = 的区别看下面的例子
A=first
B:=$(A)add
A=final
all:
@echo $(B)
运行 make 显示 firstadd
A=first
B=$(A)add
A=final
all:
@echo $(B)
运行make显示 finaladd
总结:似乎 使用:=赋值的时候没有对后面的赋值语句进行检查,而使用 =的时候会受到后面语句的影响。
语句中@echo 和echo的区别是是否显示echo。加@echo只显示输出结果不显示命令。echo会显示命令之后显示对应输出。
“ifdef”是条件关键字。语法是
ifdef <variable-name>;<text-if-true>; else <text-if-false>; endif
ifdef只检验一个变量是否被赋值,它并不会去推导这个变量,并不会把变量扩展到当前位置。
“ifeq”与“ifdef”类似。 “ifeq”语法是
ifeq (<arg1>;, <arg2>;)
,
功能是比较参数“arg1”和“arg2”的值是否相同。