gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 10:10:18 24 4
gpt4 key购买 nike

我想将一个 .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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com