linux - 如何构建内核模块的单个源文件

标签 linux linux-kernel kernel-module kbuild

我想将一个 .c 文件编译成一个 .o 文件,以便在单独的后期阶段我可以将它与其他人链接以生成可加载模块(.ko 文件)。

我尝试遵循 Kbuilds 文档 (2.4 here),但没有成功:

obj-m: myfile.o

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

输出是:

 $ make
cc    -c -o myfile.o myfile.c
myfile.c:42:26: fatal error: linux/printk.h: No such file or         directory
 #include <linux/printk.h>
                          ^
compilation terminated.
<builtin>: recipe for target 'myfile.o' failed
make: *** [myfile.o] Error 1

最佳答案

首先,由于您在这里对 header 变得致命,因此首先包含所有必需的 header 文件

#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
#include <linux/init.h> /* Needed for the macros */

第二件事,在 Makefile 中,目标应该是 modules 而不是单个 .o 文件

obj-m += myfile.o
all:
        make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

关于linux - 如何构建内核模块的单个源文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49459211/

相关文章:

linux - 如何从内核空间读取路由表信息?

linux-kernel - 黑白 kthread 和工作队列的区别

linux - 写入 I2C I/O 设备

linux - 语法错误,文件意外结束。

c++ - 线程运行时间、cpu上下文切换和性能之间的关系

Linux 内核和我的内核模块

c++ - 是否有可能检测到线程在 Linux [暂停] 中进行了上下文切换?

c - kvm:模块验证失败:缺少签名和/或所需的 key - 污染内核

LINUX awk 循环遍历 txt 文件的几行

linux - Fedora Core 中 rc.local 中错误的 insmod 如何拯救 Linux 系统?