linux/module.h: No such file or directory

2019-01-17 16:54发布

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.

3条回答
等我变得足够好
2楼-- · 2019-01-17 17:30

You may need a Makefile to compile your module.I have tried out on my personal computer(Ubuntu 10.04.4),I meet the same problem when I use gcc -c hello.c,but using a Makefile, everything will be OK.The kernel vesion is 2.6.32-54-generic

查看更多
甜甜的少女心
3楼-- · 2019-01-17 17:33

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

查看更多
趁早两清
4楼-- · 2019-01-17 17:35

Install the package kernel-devel:

yum install kernel-devel

After that, you should have:

/usr/src/kernels/$kernelversion/include/linux/module.h

You can then pass something like:

-I/usr/src/kernels/$(uname -r)/include

to the compiler

查看更多
登录 后发表回答