Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error compiling: linux/module.h: No such file or directory

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.
like image 961
Uzair Farooq Avatar asked Apr 12 '12 14:04

Uzair Farooq


5 Answers

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
like image 29
Sudip Das Avatar answered Nov 04 '22 14:11

Sudip Das


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

like image 74
Pavan Manjunath Avatar answered Nov 04 '22 13:11

Pavan Manjunath


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.

like image 3
ThiefMaster Avatar answered Nov 04 '22 14:11

ThiefMaster


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.

like image 3
jørgensen Avatar answered Nov 04 '22 13:11

jørgensen


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

like image 2
hburde Avatar answered Nov 04 '22 14:11

hburde