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

2019-04-20 00:39发布

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.

5条回答
做自己的国王
2楼-- · 2019-04-20 00:50

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

查看更多
手持菜刀,她持情操
3楼-- · 2019-04-20 00:54

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.

查看更多
三岁会撩人
4楼-- · 2019-04-20 00:58

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
查看更多
趁早两清
5楼-- · 2019-04-20 00:59

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.

查看更多
冷血范
6楼-- · 2019-04-20 01:09

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

查看更多
登录 后发表回答