I've written a simple module:
#define __KERNEL__
#define MODULE
#include <linux/kernel.h>
#include <linux/module.h>
int init_module(void)
{
printk("Hello, world\n");
return 0;
}
void cleanup_module(void)
{
printk("Goodbye\n");
}
and compiling it with this command:
cc -c hello.c
but I'm getting this error:
linux/module.h: No such file or directory
any suggestions?
EDIT: I used this commad:
cc -I/usr/src/linux-headers-3.0.0-17-generic/include -c hello.c
and it goes one step ahead, now I get this error:
In file included from /usr/src/linux-headers-3.0.0-17-generic/include/linux/kernel.h:13:0,
from hello.c:3:
/usr/src/linux-headers-3.0.0-17-generic/include/linux/linkage.h:5:25: fatal error: asm/linkage.h: No such file or directory
compilation terminated.
First thing you need the kernel sources. Many confuse user space headers and kernel space headers because many of them have the same folder structure. Most of the distros only have the user space headers and not the kernel space ones.
And generally
make
is used to build a kernel module and not a barecc
. Follow the simple step-by-step explainedHello World
kernel module given hereYou 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). Full sources are not needed (smart distros recognize this), and neither is /usr/include/whatever.
You also must use kbuild; calling
cc -I
is not enough, and has not been for more than 10 years. You start off with aKbuild
file:and a
Makefile
:and then utilize
make
.#defining
__KERNEL__
andMODULE
is also pointless, because that is already set by kbuild if needed.Source file name is basic.c
=====================================
now make file for ubuntu
At first type on ur terminal that $(uname -r) then u will get the version.. that is using on ur system
================================================
To run the code
You need the kernel headers; they are usually in
/usr/include/
if installed.Unless you are using a source-based distro or built your own kernel chances are good they are not installed by default; use your distro's package manager to install them. The package is often called
linux-headers
.Most Linux distros don't install kernel headers as default. Look for a package kernel-headers or something similar.