linux/module.h:没有这样的文件或目录 [英] linux/module.h: No such file or directory
问题描述
我是一个初学者,我正在尝试一些Linux内核编程的基础知识.今天早上,我已经在VIM中打开了module.h文件,并且在没有保存任何更改的情况下关闭了文件.之后,我将无法编译任何代码.我收到以下错误消息
i'm a beginner and i'm trying out some basics of kernel programming in linux. Today morning i've opened the module.h file in VIM, and closed without saving any changes as well. After that i'm not able to compile any of my codes. I'm getting the following error message
[root@localhost helloworld]# cc helloworld.c
helloworld.c:1:25: error: linux/module.h: No such file or directory
[root@localhost helloworld]#
这是示例代码,该代码已成功运行到最后一天.
Here is a sample code which was running successfully till the last day.
#include<linux/module.h>
#include<linux/kernel.h>
int init_module(void)
{
printk("HELLO WORLD");
return 0;
}
void cleanup_module(void)
{
printk("GOODBYE");
}
我搜索了如下的module.h文件,它确实存在
I searched for the module.h file like the following and it do exist
[root@localhost usr]# find . -name module.h
./src/kernels/2.6.18-194.el5-i686/include/asm-x86_64/module.h
./src/kernels/2.6.18-194.el5-i686/include/asm-i386/module.h
./src/kernels/2.6.18-194.el5-i686/include/linux/module.h
./include/sepol/policydb/module.h
./include/sepol/module.h
./include/kde/kunittest/module.h
[root@localhost usr]#
请帮帮我. 我在虚拟盒子中使用CentOS.
Please help me out. I'm using CentOS in virtual box.
推荐答案
您正在尝试使用纯gcc
编译模块,而没有
周围的kbuild框架.您可能已经在其中工作了
过去使用这种方法,但尝试维护它却很痛苦
使用除pure-kbuild Makefile
方法外的任何模块的模块.我有
我在与kbuild的斗争中浪费了太多时间,我也不想这样做
与您一起发生-拥抱kbuild并让它帮助您构建模块.请
在编写另一行代码之前,请先阅读Documentation/kbuild/modules.txt
.
You're trying to compile your module with plain gcc
with none of the
surrounding kbuild framework. You might have gotten something to work in the
past with this approach, but it is painful horrible awful to try to maintain
a module using anything other than pure-kbuild Makefile
approaches. I've
wasted too much of my life fighting against kbuild and I don't want the same to
happen with you -- embrace kbuild and let it help you build your module. Please
read Documentation/kbuild/modules.txt
before writing another line of code.
您需要做的是为模块创建一个Makefile
.其内容应
看起来像这样:
What you need to do is create a Makefile
for your module. Its contents should
look like this:
ifneq ($(KERNELRELEASE),)
# kbuild part of makefile
obj-m := modulename.o
else
# normal makefile
KDIR ?= /lib/modules/`uname -r`/build
default:
$(MAKE) -C $(KDIR) M=$$PWD
endif
我知道它比您经常看到的大多数Makefile
复杂得多,
但它有双重作用.如果仅在目录中运行make
,它将
从当前运行的内核中重新调用make
以使用kbuild机制
(假设至少从/lib/modules/.../build
到
正确的位置.)
I know it's a lot more complicated than most Makefile
s you're used to seeing,
but it serves a dual-purpose. If you just run make
in your directory, it'll
re-invoke make
to use the kbuild mechanism from the currently-running kernel
(assumed to at least have a symlink from /lib/modules/.../build
to the
correct location).
重新调用的make
命令($(MAKE)
)将正确构建您的模块,并
为您节省了比您所能欣赏的更多的时间. (真的.)
The re-invoked make
command ($(MAKE)
) will properly build your module and
save you more time than you can ever appreciate. (Really.)
在进行此项工作时,请保持Documentation/kbuild/modules.txt
在你身边.
Keep Documentation/kbuild/modules.txt
by your side while making this work.
注意:Documentation/kbuild/modules.txt
可能在您的Linux系统上的/usr/share/linux-headers-$(uname -r)/Documentation/kbuild/modules.txt
Note: Documentation/kbuild/modules.txt
may be available at your linux system at /usr/share/linux-headers-$(uname -r)/Documentation/kbuild/modules.txt
这篇关于linux/module.h:没有这样的文件或目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!