在编译内核模块时,Makefile 里一般会写 “make -C /lib/modules/`uname -r`/build M=`pwd` modules” 这句话。其中 -C 选项来自 make 命令本身,它后接一个目录名,这里表示内核源码所在目录 。
M 变量 和 SUBDIRS 变量都来自内核顶层 Makefile ,定义如下:
[Plain Text] 纯文本查看 复制代码 # Use make M=dir to specify directory of external module to build
# Old syntax make ... SUBDIRS=$PWD is still supported
# Setting the environment variable KBUILD_EXTMOD take precedence
ifdef SUBDIRS
KBUILD_EXTMOD ?= $(SUBDIRS)
endif
ifeq ("$(origin M)", "command line")
KBUILD_EXTMOD := $(M)
endif
从注释知道,SUBDIRS 变量在老式风格中使用,现在都用 M 变量来指定要编译内核模块代码所在路径。也就是说以下两句是等效的:make -C /lib/modules/`uname -r`/build M=`pwd` modules 和make -C /lib/modules/`uname -r`/build SUBDIRS=`pwd` modules
如果同时在命令行里同时指定了 SUBDIRS 和 M,那么被编译的只有 M 所目录下指定的模块代码,而 SUBDIRS 的代码不会编译,这是因为 Makefile 文件从上至下解析,KBUILD_EXTMOD 变量的值最终会被 $(M) 所覆盖。 |