简体   繁体   English

如何编译内核模块

[英]how to compile a kernel module

I'm trying to compile a simple hello world module following this guide and I'm confused about what the Makefile is actually doing. 我正在尝试按照本指南编译一个简单的hello world模块,我对Makefile实际上在做什么感到困惑。

obj-m += hello-1.o

all:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

I understand that when i type the make command it will run the all recipe which runs make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules . 据我所知,当我输入make命令时,它将运行运行make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modulesall配方。 So now it runs the Makefile found at the path given after the -C flag but what does the M=$(PWD) modules do? 所以现在它运行在-C标志之后给出的路径上找到的Makefile但是M=$(PWD) modules做了什么?

  1. ' obj-m ' :- specifies object files which are built as loadable kernel modules. ' obj-m ': - 指定构建为可加载内核模块的目标文件。
  2. ' all and clean ' :- If you run 'make' by default it will run "all :". ' all and clean ': - 如果你默认运行'make',它将运行“all:”。 But we can use all and clean with make. 但我们可以使用all和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.' 3.” uname -r ' :- get name and information about current kernel. uname -r ': - 获取有关当前内核的名称和信息。

 Example :- for me, my kernel is "4.6.0-rc1".
  1. Option ' -C dir ' :- Change to directory dir before reading the makefiles. 选项' -C dir ': - 在读取makefile之前更改目录dir。

     Example :- "make -C /lib/modules/$(shell uname -r)/build" will change to "make -C /lib/modules/4.6.0-rc1/build. 
  2. ' $pwd ':- get the path of your current directory. ' $ pwd ': - 获取当前目录的路径。

Now you want to create your loadable module by using " make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules". 现在,您希望使用“ make -C / lib / modules / $(shell uname -r)/ build M = $(PWD) modules”来创建可加载模块。

Your source code need environment to run. 您的源代码需要运行环境。 That's why we have to use -C option to change build directory. 这就是我们必须使用-C选项来更改构建目录的原因。 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. 哪些都需要定义,头文件,宏等等。现在更改为构建目录后,您需要告诉您的模块在哪里,这就是我们使用M = $ PWD的原因。

To compile kernel module, the make command you normally need is in this form: 要编译内核模块,您通常需要的make命令采用以下形式:

make -C /lib/modules/3.16.0-70-generic/build M=/home/test/ldd3/hello modules

in which, -C means switching to another path. 其中, -C表示切换到另一条路径。

/lib/modules/3.16.0-70-generic/

is the path to the kernel in used, and 是使用中的内核的路径,和

/home/test/ldd3/hello

is where the source of your module resides. 是模块源所在的位置。

what does the M=$(PWD) modules do? M = $(PWD)模块有什么作用?

So as I said M=$(PWD) is simply a shell variable that stores the current path to your kernel module. 所以我说M=$(PWD)只是一个shell变量,它存储了内核模块的当前路径。 make needs to store this as it switches to the kernel build path. make需要在切换到内核构建路径时存储它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM