linux/module.h: 没有那个文件或目录 [英] linux/module.h: No such file or directory

查看:33
本文介绍了linux/module.h: 没有那个文件或目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是初学者,正在尝试一些 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框架.你可能已经得到了一些工作过去使用这种方法,但尝试维护它是痛苦可怕的使用纯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 Makefiles 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/模块.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆