当前位置:网站首页>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 .
边栏推荐
- 如何看懂开发的查询语句
- How to realize the four isolation levels of MySQL (brief)
- redis常识
- 伸展树(一) - 概念和C实现
- Tianrunyun, invested by Tian Suning, was listed: its market value was 2.2 billion Hong Kong, and its first year profit decreased by 75%
- 二叉堆(一) - 原理与C实现
- The developer said, "this doesn't need to be tested, just return to the normal process". What about the testers?
- kafuka学习之路(一)kafuka安装和简单使用
- Several cases of index failure
- Unittest 框架介绍及第一个demo
猜你喜欢
Mingchuang plans to be listed on July 13: the highest issue price is HK $22.1, and the net profit in a single quarter decreases by 19%
达梦数据冲刺科创板:拟募资24亿 冯裕才曾为华科教授
TEMPEST HDMI泄漏接收 4
Why must we move from Devops to bizdevops?
Numpy的矩阵
About keil compiler, "file has been changed outside the editor, reload?" Solutions for
Wonderful! MarkBERT
Matrix of numpy
Neo4j 中文开发者月刊 - 202206期
Introduction to unittest framework and the first demo
随机推荐
Value/string in redis
用于分类任务的数据集划分脚本
Raspberry pie 4B installation tensorflow2.0[easy to understand]
Binary stack (I) - principle and C implementation
证券账户销户后果 开户安全吗
Goldfish rhca memoirs: do447 uses ansible to communicate with API -- using ansible tower API to start jobs
No statements may be issued when any streaming result sets are open and in use on a given connection
Share the method of how to preview PSD format and PSD file thumbnail plug-in [easy to understand]
Why must we move from Devops to bizdevops?
About keil compiler, "file has been changed outside the editor, reload?" Solutions for
Paxos 入门
CAD如何设置标注小数位
kafuka学习之路(一)kafuka安装和简单使用
Continuous delivery -pipeline getting started
Redis configuration environment variables
伸展树(一) - 概念和C实现
Wonderful! MarkBERT
CPU 上下文切换的机制和类型 (CPU Context Switch)
编译调试Net6源码
activity工作流引擎