先给出两个makefile相关链接:
http://www.yayu.org/book/gnu_make/make-04.html
http://www.yayu.org/book/gnu_make/make-06.html
例1:linux-2.6.34.14\fs\ext2\makefile详解
# # Makefile for the linux ext2-filesystem routines. # obj-$(CONFIG_EXT2_FS) += ext2.o ext2-y := balloc.o dir.o file.o ialloc.o inode.o \ ioctl.o namei.o super.o symlink.o ext2-$(CONFIG_EXT2_FS_XATTR) += xattr.o xattr_user.o xattr_trusted.o ext2-$(CONFIG_EXT2_FS_POSIX_ACL) += acl.o ext2-$(CONFIG_EXT2_FS_SECURITY) += xattr_security.o ext2-$(CONFIG_EXT2_FS_XIP) += xip.o
1.obj-$(CONFIG_EXT2_FS) += ext2.o obj- 表示如何编译 y m n分别表示进入内核编译,m 模块编译,n不编译。其中$(CONFIG_EXT2_FS)表示取CONFIG_EXT2_FS值操作,所以ext2的编译方式取决于编译内核是make config时CONFIG_EXT2_FS选定的值。
2.ext2-y或者ext2-objs都表示ext2模块(注:-objs用法见例二)
3.:= 表示赋值操作
4.ext2-y := balloc.o dir.o file.o ialloc.o inode.o \
5.+= 表示追加方式复值:
objects = main.o foo.o bar.o utils.o
objects += another.o
上边的两个操作之后变量“objects”的值成:“main.o foo.o bar.o utils.o another.o”。使用“+=”操作符,就相当于:
objects = main.o foo.o bar.o utils.o
objects := $(objects) another.o
6.ext2-$(CONFIG_EXT2_FS_XATTR) += xattr.o xattr_user.o xattr_trusted.o 表示如果make config步骤中CONFIG_EXT2_FS_XATTR赋值为y,则ext2模块在前面的编译基础上再加上xattr.o xattr_user.o xattr_trusted.o三个文件。
7ext2-$(CONFIG_EXT2_FS_POSIX_ACL) += acl.o
# # Makefile for the Linux pcmfs filesystem routines. # obj-m += simplefs.o simplefs-objs := bitmap.o itree_common.o namei.o inode.o file.o dir.o KERNELDIR:=/usr/src/linux-2.6.34.14/ PWD:=$(shell pwd) default: make -C $(KERNELDIR) M=$(PWD) modules clean: rm -rf *.o *.mod.c *.ko *.symvers
1.obj-m直接指定编译方式为模块化
2.simplefs-objs := bitmap.o itree_common.o namei.o inode.o file.o dir.o 其中-objs为模块。表示simplefs.o模块依赖后面的文件。
3.KERNELDIR:=/usr/src/linux-2.6.34.14/ 将变量KERNELDIR赋值,为后面提供使用
4.PWD:=$(shell pwd)赋值为当前目录
5.make -C $(KERNELDIR) M=$(PWD) modules
其中make的 -C表示转换路径,可以再linux下通过man make查看:
-C dir, --directory=dir