1

在 Kbuild 树中,当我们编写一个简单的 hello.ko 程序时,为什么我们需要在构建规则中使用 -C /lib/module/。为什么这是必需的?可以单独建吗?它的目的是什么?

4

2 回答 2

2

while building kernel modules why do we need /lib/modules?

提供上述选项不是强制性的,即/lib/modules主要目的是get configured source-code directory

您可以设置为直接配置的源代码,或者您可以在上面提供,即/lib/modules/,它具有用于构建源代码的软链接。

KDIR,

您可以设置完整的内核源目录(配置和编译)或只设置内核头目录(最少需要)。两种解决方案

1)Full kernel sources

2)Only kernel headers (linux-headers-* packages in Debian/Ubuntu distributions)

在这两种情况下源或标题必须是configured.Manymacros or functions depend on the configuration

-C选项调用kernel Makefile,传递module directory in the M variable ,the kernel Makefile knows how to compile a module.

例如,如果您配置内核,Arm architecture or machine那么configured kernel Makefile将告诉您如何编译以及应该为哪种架构构建模块。

要编译,一个内核模块needs access to the kernel headers, containing the defnitions of functions, types and constants

Can it be build solely?

不,您不能单独构建,因为您的模块应该知道要构建哪个内核以及需要编译哪个配置,以便在插入模块时所有相应的符号和配置都应该匹配。这一切都可以通过toplevel Makefile of configured kernel.

于 2013-11-06T09:18:33.223 回答
1

要构建模块,通常遵循您在 make 文件中编写的命令

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

在上面 -C 用于在读取 makefile 之前更改目录。因此,当 make 执行时,它会转到目录 /lib/modules/$(shell uname -r)/build。

在 Linux 中构建通常是一个软链接到内核源代码或需要构建模块的源代码。

从该源,make 命令读取内核 make 文件并构建您的模块。

于 2013-11-06T08:50:42.720 回答