如何在 Linux 源代码中包含用户级 C 程序以使用 Linux 内核编译?



我有一个与特定版本的Linux内核源代码相关联的C程序。
我只想针对内核源代码编译应用程序,然后在 shell 中运行二进制文件。
在构建 Linux 内核后,我将作为用户空间应用程序运行代码,而不是在内核空间中运行代码。

我如何以及在哪里可以将此程序包含在 Linux 源代码中,以便可以使用 Linux 内核对其进行编译?

用户空间程序确实存在于tools/子目录的 Linux 内核源代码树中。
对于什么样的程序构成需要/应该与内核源代码一起包含/分发的"工具",似乎没有一个明确的(或任何(定义。

内核源代码树中(当前(存在的实用程序类型包括一个用于检查内存页状态位(tools/vm/page-types.c(的管理员程序,以及三个使用/演示("新"(chardev GPIO接口(tools/gpio/gpio-event-mon.c等(的简单程序。

内核源代码中最大的用户空间程序或工具类别位于tools/testing/selftests/内核子目录中。
该文档位于 Documentation/dev-tools/kselftest.rst 中

======================
Linux Kernel Selftests
======================
The kernel contains a set of "self tests" under the tools/testing/selftests/
directory. These are intended to be small tests to exercise individual code
paths in the kernel. Tests are intended to be run after building, installing
and booting a kernel.  
...  
kselftest runs as a userspace process.  Tests that can be written/run in
userspace may wish to use the `Test Harness`_.  Tests that need to be
run in kernel space may wish to use a `Test Module`_.

或者,有许多内核子系统和硬件组件在内核源代码中没有其工具,而是将该代码作为单独的源代码/项目包提供。

鉴于 Linux 内核向用户空间提供的二进制 API 的稳定性,程序很少绑定到特定的内核版本。 内核维护者尽可能避免回归(即导致现有用户中断的更改(。

将程序包含在内核源代码中的一个原因似乎是内核维护者的方便。
鼓励内核构建者也构建和运行自测程序。

最新更新