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.
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-genericYou'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-kbuildMakefile
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 readDocumentation/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: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 runmake
in your directory, it'll re-invokemake
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
Install the package kernel-devel:
After that, you should have:
You can then pass something like:
to the compiler