如何编译内核模块 [英] how to compile a kernel module

查看:135
本文介绍了如何编译内核模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按照指南,我对Makefile实际在做什么感到困惑.

I'm trying to compile a simple hello world module following this guide and I'm confused about what the Makefile is actually doing.

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

我了解,当我键入make命令时,它将运行运行make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modulesall配方.因此,现在它运行在-C标志后给定路径下找到的Makefile,但是M=$(PWD) modules会做什么?

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. So now it runs the Makefile found at the path given after the -C flag but what does the M=$(PWD) modules do?

推荐答案

  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.

    这篇关于如何编译内核模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆