Linux驱动模块编译

1.
1.1.模块上层
1.2.内核模块
2.解析
2.1 定义变量:
内核top 所在路径 。
模块所在的文件夹路径 。
定义编译工具链 。
定义当前的目标架构平台 。
内核中应用的头文件所在的路径 。
表示在编译模块时,需要添加的目录 。编译器会从这些目录中找到所需要的头文件 。
补充:如果在模块中引用了其他模块导出到内核符号表的函数,则需要包含该模块产生的符号表文件. 。例如,模块中如果引用了同级文件夹中另一模块的函数则需要在模块增加这一行定义:=$(obj)/..//.
2.2 指定编译的目标文件
obj-m:=.o表示把文件.o作为"模块"进行编译,不会编译到内核,但是会生成一个独立的 ".ko" 文件;
如果有一个模块名为 .ko, 是来自多个源文件(如file1.c 和 file2.c ), 正确的书写应当是:
obj-m := .o
-objs := file1.o file2.o
第19行即是定义模块.ko。
2.3 模块编译
第10行 ~ 11行,编译模块 。
2.4 模块装载
第12行 ~ 13行,指令是把编译的ko拷贝到根目录下的/lib//4.1.15/目录中 。指定的安装目录主要是为了放置需要安装的镜像和map(符号表)文件,系统的启动需要这些文件的参与 。

Linux驱动模块编译

文章插图
2.5 清除编译中间文件和编译结果
第15行 ~ 16行,指令是清除编译中间文件和编译结果 。相当于以下指令:
rm -f *.ko *.o *.mod.o *.mod.c *. *.order
2.6 PHONY
.PHONY是一个伪目标,可以防止在中定义的目标和工作目录下的实际文件出现名字冲突,提高执行时的效率 。
2.6 执行逻辑
这个在一次执行中要被读 2 次 。
是在内核源码的顶层中定义的一个变量,在第一次读取执行此时,没有被定义,所以make将读入、执行ifeq之后的内容,如果make的目标是clean,直接执行clean操作,然后结束 。当make的目标为all时,-C $()指明跳转到内核源码目录下读取那里的;M=$() 表明返回到当前目录继续读入、执行当前的 。当从内核源码目录返回时,已被定义,make将继续读取else之后的内容 。else之后的内容为语法的语句,指明模块源码中各文件的依赖关系,以及要生成的目标模块名,被启动去解析语法的语句 。obj-m := .o表示编译连接后将生成.ko模块 。
3. obj-m等在//.txt中的定义
3.1 obj-m := .o
在.txt中提到:
Thewill build .o from .c,
and, after , willin the.ko.
The above line can be put ina "" file or a "."
When theis built from, anline is
the files:
-y := .o .o ...
3.2 -C $KDIR and M=$PWD
-C $KDIR
Thewhere theis .
"make" willto the
whenand willback when .
M=$PWD
that anis being built.
The value given to "M" is thepath of the
where the( file) is
.
3.3
Whenan, only aof the "make" are .
make -C $KDIR M=$PWD []
Thewill build the (s)in the
, so adoes not need to be . All
files will also bein this . No
are made tothe, and it is a
that a"make" has beenfor the
.
Thefor. It has the
sameas if nowas . See
above.
3.4
the(s). Theis
/lib///extra/, but amay
be added with( in5).
3.5 clean
allfiles in theonly.
【Linux驱动模块编译】3.6 help
List thefor.
参考: