12

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.
Uzair Farooq
  • 917
  • 3
  • 15
  • 25

5 Answers5

11

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 bare cc. Follow the simple step-by-step explained Hello World kernel module given here

Pavan Manjunath
  • 27,404
  • 12
  • 99
  • 125
  • thanks a lot... I made the make file and it worked. Thanks again – Uzair Farooq Apr 12 '12 at 15:47
  • @PavanManjunath I have created a makefile just as shown in tutorial but I am getting this message make: Nothing to be done for `all'. I am using ubuntu and have linux-header installed. – user606669 Oct 16 '13 at 17:27
  • 1
    @user606669 There can be many reasons for your problem. See if this helps http://ubuntuforums.org/showthread.php?t=1795434 or may be this http://stackoverflow.com/questions/8561640/make-nothing-to-be-done-for-all – Pavan Manjunath Oct 17 '13 at 09:26
  • Thanks I'll try and will post the result here – user606669 Oct 18 '13 at 10:52
4

Source file name is basic.c

#include <linux/init.h>
#include <linux/module.h>
/*MODULE_LICENSE("Dual BSD/GPL");*/
static int hello_init(void)
{
    printk(KERN_ALERT "Hello, world\n");
    return 0;
}
static void hello_exit(void)
{
    printk(KERN_ALERT "Goodbye, cruel world\n");
}
module_init(hello_init);
module_exit(hello_exit);

=====================================

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

obj-m +=basic.o

KDIR =//usr/src/linux-headers-3.13.0-44-generic

all:
 $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
clean:
 rm -rf *.o *.ko *.mod.* *.symvers *.order

================================================

To run the code

$sudo insmod basic.ko
$dmesg
u will get the output
$sudo rmmod basic.ko
$dmesg
themadmax
  • 2,344
  • 1
  • 31
  • 36
Sudip Das
  • 1,178
  • 1
  • 9
  • 24
3

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.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
3

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). 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 a Kbuild file:

obj-m += mymodule.o

and a Makefile:

kdir=/lib/modules/$(shell uname -r)/build
all:
        make -C ${kdir} M=$$PWD
modules_install clean:
        make -C ${kdir} M=$$PWD $@

and then utilize make.

#defining __KERNEL__ and MODULE is also pointless, because that is already set by kbuild if needed.

jørgensen
  • 10,149
  • 2
  • 20
  • 27
2

Most Linux distros don't install kernel headers as default. Look for a package kernel-headers or something similar.

hburde
  • 1,441
  • 11
  • 6