简体   繁体   English

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

[英]How to build a single source file of a kernel module

I want to compile a .c file into an .o file so that in a separate later stage I could link it with others to produce a loadable module ( .ko file). 我想将.c文件编译为.o文件,以便在以后的单独阶段中可以将其与其他文件链接以生成可加载模块( .ko文件)。

I tried to follow Kbuilds documentation (2.4 here ), but had no success: 我尝试遵循Kbuilds文档( 此处为 2.4),但没有成功:

obj-m: myfile.o

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

The output is: 输出为:

 $ 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

First thing, as you are getting fatal here regarding headers, so first include all required header file 首先,由于您在此处遇到关于头的致命问题,因此首先包括所有必需的头文件

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

Second thing, in Makefile target should be modules instead of single .o file 第二件事,在Makefile目标应该是modules而不是单个.o文件

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM