当前位置:网站首页>Building external modules
Building external modules
2022-07-01 11:35:00 【Bug cash capability】
Building external modules (Building External Modules)
- This document describes how to build an out of tree kernel module .(This document describes how to build an out-of-tree kernel module.)
1) brief introduction (Introduction)
- “kbuild” yes Linux The build system used by the kernel . Modules must use kbuild To be compatible with changes in building infrastructure , And for “gcc” Choose the right logo . It provides the function of building modules inside and outside the tree . The method of building both is similar , All modules were originally developed and built outside the tree .(“kbuild” is the build system used by the Linux kernel. Modules must use kbuild to stay compatible with changes in the build infrastructure and to pick up the right flags to “gcc.” Functionality for building modules both in-tree and out-of-tree is provided. The method for building either is similar, and all modules are initially developed and built out-of-tree.)
2) How to build external modules (How to Build External Modules)
- To build external modules , You must have a pre built kernel available , It contains the configuration and header files used in the build . Besides , The kernel must be built with modules enabled . If you are using a distribution kernel , Your distribution will provide a package for the kernel you are running .(To build external modules, you must have a prebuilt kernel available that contains the configuration and header files used in the build. Also, the kernel must have been built with modules enabled. If you are using a distribution kernel, there will be a package for the kernel you are running provided by your distribution.)
- Another way is to use “make” The goal is “modules_prepare”. This will ensure that the kernel contains the required information . This goal exists only as a simple method , You can prepare the kernel source tree for building external modules .(An alternative is to use the “make” target “modules_prepare.” This will make sure the kernel contains the information required. The target exists solely as a simple way to prepare a kernel source tree for building external modules.)
- Be careful : Even if it's set CONFIG_MODVERSIONS,“modules_prepare” Will not build Module.symvers; therefore , You need to perform a complete kernel build to make the module version control work .(NOTE: “modules_prepare” will not build Module.symvers even if CONFIG_MODVERSIONS is set; therefore, a full kernel build needs to be executed to make module versioning work.)
2.1 Command syntax (Command Syntax)
The command to build external modules is :(The command to build an external module is:)
$ make -C <path_to_kernel_src> M=$PWD
Due to the “M=<dir>” Options ,kbuild The system knows that external modules are being built .(The kbuild system knows that an external module is being built due to the “M=<dir>” option given in the command.)
Build for the running kernel , Please use :(To build against the running kernel use)
$ make -C /lib/modules/`uname -r`/build M=$PWD
2.2 Options (Options)
$KDIR Refers to the path of the kernel source directory .($KDIR refers to the path of the kernel source directory.)
make -C $KDIR M=$PWD
2.3 The goal is (Targets)
When building external modules , There's only a part of it “make” Target available .(When building an external module, only a subset of the “make” targets are available.)
make -C $KDIR M=$PWD [target]
all:
make -C /lib/modules/$(shell uname -r)/build/ M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build/ M=$(PWD) clean
The standard make -C The instructions are like this :
make -C $KDIR M=$PWD [target], The following describes the meaning of each field .
-C Options :make -C To enter into -C Compile in the directory corresponding to the parameter , The corresponding is also under the resolution target directory
Makefile, When compilation is complete, return .
$KDIR:/lib/modules/$(shell uname -r)/build/, Specify the location of the kernel source code , Indicates that the root directory of the kernel source code
Compile .
When compiling directly on the target board , By default, the kernel header file is stored in /lib/modules/$(shell uname -r)/build/ in , This
build/ Directory is a soft connection , Link to the installation location of the source dock file . The real source code library of the kernel directly refers to the running kernel mirror
image . When compiling across platforms , You need to specify the corresponding kernel source code directory , Instead of the source directory in the system , When cross compiling at the same time , You need to specify the architecture platform and cross compilation tool chain ;
M=$(PWD): Represents the directory that needs to be compiled in the kernel , In the example, it is the current directory .
modules, Belong to [target] part , in fact , This is an optional option . The default behavior is to compile the source file and generate the kernel module , namely
module(s), But it also supports the following options :
modules_install: Install this external module , The default installation address is /lib/modules/$(uname -r)/extra/, Also can
By built-in variables INSTALL_MOD_PATH Specify the installation directory
clean: Uninstall the files generated by the compilation process under the source file directory , Above Makefile The last line shows .
help: Help information
2.4 Build a separate file (Building Separate Files)
make -C $KDIR M=$PWD bar.lst
make -C $KDIR M=$PWD baz.o
make -C $KDIR M=$PWD foo.ko
make -C $KDIR M=$PWD ./
3) Create for external modules Kbuild file (Creating a Kbuild File for an External Module)
obj-m := <module_name>.o
kbuild The system will start from <module_name>.c structure <module_name>.o, And generate kernel modules after linking <module_name>.ko. The line above can be placed in “Kbuild” File or “Makefile” in . When modules are built from multiple sources , An extra line is needed to list the files :(The kbuild system will build <module_name>.o from <module_name>.c, and, after linking, will result in the kernel module <module_name>.ko. The above line can be put in either a “Kbuild” file or a “Makefile.” When the module is built from multiple sources, an additional line is needed listing the files:)
<module_name>-y := <src1>.o <src2>.o ......
Compile multiple source files
hello_world It's always simple , But in actual development , There will be more complicated situations , At this time, we need to know more Makefile Options :
First , When one .o When the generation of the target file depends on multiple source files , obviously make The automatic derivation rule of is not enough ( It can only be derived from the same name , For example compiler filename.o, Just look for filename.c ), We can specify :
obj-m += hello.o
hello-y := a.o b.o hello_world.o
hello.o The target file depends on a.o, b.o, hello_world.o, So here a.o and b.o If no source file is specified , According to the derivation rule, it depends on the source file a.c, b.c, hello_world.c.
except hello-y, You can also use hello-objs, The effect is the same .
Compile multiple loadable modules at the same time
kbuild Support to compile multiple loadable modules at the same time , That is, generate multiple .ko file , It's in this format :
obj-m := foo.o bar.o
foo-y := <foo_srcs>
bar-y := <bar_srcs>
Placement of external module header files
When the compiled object module depends on multiple header files ,kbuild There are such regulations for the placement of header files :
Put it directly on Makefile Under the same directory , At compile time, the current directory will be added to the header file search directory . Put it in the system directory , This system directory is in the source code directory include/linux/, Note that the source code directory is not the system directory . And universal Makefile equally , Use -I$(DIR) To specify the , The difference is , Variables representing compilation options are fixed , by ccflag, Because the current Makefile It is equivalent to inlaying Kbuild Compile in the system , So we must follow Kbuild The provisions of .
The general usage is like this :
.
|__ src
| |__ complex_main.c
| |__ hal
| |__ hardwareif.c
| |__ include
| |__ hardwareif.h
|__ include
|__ complex.h
--> filename: Kbuild
obj-m := complex.o
complex-y := src/complex_main.o
complex-y += src/hal/hardwareif.o
ccflags-y := -I$(src)/include
ccflags-y += -I$(src)/src/hal/include
ccflags-y := -I$(DIR)/include
kbuild Will be $(DIR)/include Directory is added to the header file search directory at compile time .
边栏推荐
猜你喜欢
用实际例子详细探究OpenCV的轮廓检测函数findContours(),彻底搞清每个参数、每种模式的真正作用与含义
2022/6/30学习总结
Istio、eBPF 和 RSocket Broker:深入研究服务网格
Unittest框架中跳过要执行的测试用例
Several cases of index failure
"Target detection" + "visual understanding" to realize the understanding and translation of the input image (with source code)
y48.第三章 Kubernetes从入门到精通 -- Pod的状态和探针(二一)
邻接矩阵无向图(一) - 基本概念与C语言
redis配置环境变量
Learning summary on June 30, 2022
随机推荐
241. 为运算表达式设计优先级 : DFS 运用题
How to set decimal places in CAD
TEMPEST HDMI泄漏接收 3
Spam filtering challenges
Redis common sense
redis中value/list
Raspberry pie 4B installation tensorflow2.0[easy to understand]
Impressive bug summary (continuously updated)
MySQL IN 和 NOT IN () 空列表报错
【MAUI】为 Label、Image 等控件添加点击事件
Tempest HDMI leak reception 4
Can I open an account today and buy stocks today? Is it safe to open an account online?
Brief analysis of edgedb architecture
The developer said, "this doesn't need to be tested, just return to the normal process". What about the testers?
Flip the array gracefully
图的理论基础
名创拟7月13日上市:最高发行价22.1港元 单季净利下降19%
2022/6/28学习总结
Tempest HDMI leak receive 3
sshd_config 中 PermitRootLogin 的探讨