Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
648 views
in Technique[技术] by (71.8m points)

kernel - linux/module.h: No such file or directory

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");
}

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]# 

Please help me out. I'm using CentOS in virtual box.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

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).

The re-invoked make command ($(MAKE)) will properly build your module and save you more time than you can ever appreciate. (Really.)

Keep Documentation/kbuild/modules.txt by your side while making this work.

Note: Documentation/kbuild/modules.txt may be available at your linux system at /usr/share/linux-headers-$(uname -r)/Documentation/kbuild/modules.txt


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...