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.
h . That file is provided by kernel header packages, e.g. on Debian derivatives, the linux-headers-$(uname -r) package. The /usr/include/linux/kernel. h is intended for user processes, not for kernel modules.
These macros are defined in linux/init. h and serve to free up kernel memory. When you boot your kernel and see something like Freeing unused kernel memory: 236k freed, this is precisely what the kernel is freeing. This macro served the same purpose as __init, but is now very deprecated in favor of __init.
There ARE some linux headers there (/usr/include/linux), but module.h is not one of them, as it deals with kernel constructs directly. In short, you need a Makefile. Also, get rid of the #define MODULE. save the following to a file called Makefile and run make: This triggers the kernel build system.
This file does not exist, because in Linux, by default you are developing a user space application. These have very limited access to any system-wide interrupts. The file in your /usr/src/linux*/include/linux, is a kernel header. It is not usable by user-space apps. It is usable only for kernel modules.
In short, /usr/include/linux contains the headers for the user-space processes to deal with the kernel API. While the /path-to-kernel-source/include/linux contains the internal header files used by the kernel. Linux kernel developers face them mostly if he develops kernel modules.
You need the kernel build environment (selection of scripts, header and Makefiles) usually this is reachable through /lib/modules/ version /build (a symlink to it) if a kernel has been installed already. Otherwise, the directory is the build directory (the one where System.map is in).
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 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).
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With