且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何编译内核模块

更新时间:2022-06-24 03:07:25

  1. ' obj-m ':-指定构建为可加载的目标文件 内核模块.
  2. '所有并清理':-如果默认运行"make",它将运行"all:". 但是我们可以全部使用make进行清理.它只会运行那些特定的命令.

  1. 'obj-m' :- specifies object files which are built as loadable kernel modules.
  2. 'all and clean' :- If you run 'make' by default it will run "all :". But we can use all and clean with make. it will run only those specific command.

Example :-
        'make all' will run "make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules"
        'make clean will run "make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean"

3.' uname -r ':-获取有关当前内核的名称和信息.

3.'uname -r' :- get name and information about current kernel.

 Example :- for me, my kernel is "4.6.0-rc1".

  1. 选项" -C目录":-在读取makefile之前更改为目录dir.

  1. Option ‘-C dir’ :- Change to directory dir before reading the makefiles.

Example :- "make -C /lib/modules/$(shell uname -r)/build" will change to "make -C /lib/modules/4.6.0-rc1/build.

  • ' $ pwd ':-获取当前目录的路径.
  • '$pwd':- get the path of your current directory.
  • 现在,您要使用" make -C/lib/modules/$(shell uname -r)/build M = $(PWD)模块"来创建可加载模块.

    Now you want to create your loadable module by using "make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules".

    您的源代码需要运行环境.这就是为什么我们必须使用-C选项更改构建目录的原因.这些都具有所需的定义,头文件,宏等.现在,在更改构建目录后,您需要告诉您模块所在的位置,这就是我们使用M = $ PWD的原因.

    Your source code need environment to run. That's why we have to use -C option to change build directory. Which have all needed definition, header file, Macro and etc. Now after changing to build directory you need to tell where is your module present, that's why we are using M=$PWD.