Module 主内核是否;制造;命令是否也在内部生成模块?

Module 主内核是否;制造;命令是否也在内部生成模块?,module,linux-kernel,linux-device-driver,Module,Linux Kernel,Linux Device Driver,我正在学习如何编写内核驱动程序,这是我第一次尝试构建内核驱动程序。我已经为我的模块文件创建了一个文件夹drivers/naveen/,Kconfig和Makefile。以下是这些文件的内容: Kconfig config HELLO_WORLD tristate "Hello World support" default m ---help--- This option enables printing hello world

我正在学习如何编写内核驱动程序,这是我第一次尝试构建内核驱动程序。我已经为我的模块文件创建了一个文件夹
drivers/naveen/
Kconfig
Makefile
。以下是这些文件的内容:

Kconfig

config HELLO_WORLD
        tristate "Hello World support"
        default m
        ---help---
          This option enables printing hello world
生成文件

obj-$(CONFIG_HELLO_WORLD) += hello.o
你好,c

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>

static int __init hello_init(void)
{
    printk(KERN_ERR "This is NAVEEN module");
    return 0;
}

static int __exit hello_exit(void)
{
    printk(KERN_ERR "NAVEEN exiting module");
    return 0;
}

module_init(hello_init);
module_exit(hello_exit);
MODULE_AUTHOR("Naveen");
MODULE_LICENSE("GPL");
以及
驱动程序/Kconfig
中的以下行:

source "drivers/naveen/Kconfig"
我生成的
.config
包含
config\u HELLO\u WORLD=m

我做了
make ARCH=x86_64-j16
,我可以看到生成的
hello.ko
。为什么?我希望只有当我将
make modules
设置为在
.config
中使用
m
进行模块化时,才能生成它,而不是使用just make进行编译。有人能向我解释一下我的行为或我做错了什么吗


这是否意味着
make
也会
make模块
。我可以从
make help
中看出
make
实际上意味着
make all
,因此它在内部也应该做
make modules
,所以一旦make成功就不需要做
make modules

你没有做错什么。自从内核版本2.6.0(我认为实际上是内核版本2.5.60)以来,
modules
目标一直是
all
目标的依赖项

添加模块的方法是将其添加到内核源代码树中。也可以在内核源代码树之外构建自定义模块,即所谓的“树外”内核模块。通常,这些不需要Kconfig文件,Makefile中的
obj-$(CONFIG\u HELLO\u WORLD)
将替换为
obj-m

以下是“hello”模块的“树外”版本的生成文件:

这个Makefile使用了一个常见的技巧,因此可以将同一个Makefile作为“普通”Makefile和“kbuild”Makefile调用。
else
endif
行之间的“正常”部分调用内核的Makefile(其位置由
KDIR
变量指定)上的
$(MAKE)
,告诉它在当前目录中构建
模块
目标(由
M=`pwd`
指定)。“kbuild”部分位于
ifneq($(KERNELRELEASE),)
else
行之间,并且是用于构建内核部分的正常“kbuild”格式


这个技巧取决于
KERNELRELEASE
变量最初未设置或为空。它将由内核的Makefile规则设置为非空值。

Linux内核由两部分组成-核心内核和模块。当我们只做
make
时,它意味着
makeall
,这意味着
makevmlinux和&makemodules
。因此,如果我们已经执行了
make
,我们就不需要再次执行
makemodules
,只需运行命令
makemodules\u install
,而不必执行
makemodules

source "drivers/naveen/Kconfig"
ifneq ($(KERNELRELEASE),)
# Kbuild part of Makefile

obj-m += hello.o

else
# Normal part of Makefile
#

# Kernel build directory specified by KDIR variable
# Default to running kernel's build directory if KDIR not set externally
KDIR ?= "/lib/modules/`uname -r`/build"

all:
    $(MAKE) -C "$(KDIR)" M=`pwd` modules

clean:
    $(MAKE) -C "$(KDIR)" M=`pwd` clean

endif