错误编译:linux/module.h:没有这样的文件或目录

Uza*_*ooq 12 c c++ linux kernel compilation

我写了一个简单的模块:

#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");
}
Run Code Online (Sandbox Code Playgroud)

并使用此命令编译它:

cc -c hello.c
Run Code Online (Sandbox Code Playgroud)

但我收到这个错误:

 linux/module.h: No such file or directory
Run Code Online (Sandbox Code Playgroud)

有什么建议?

编辑:我用过这个commad:

cc -I/usr/src/linux-headers-3.0.0-17-generic/include -c hello.c
Run Code Online (Sandbox Code Playgroud)

它领先一步,现在我收到这个错误:

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.
Run Code Online (Sandbox Code Playgroud)

Pav*_*ath 10

首先你需要内核源代码.许多人混淆了用户空间头和内核空间头,因为它们中的许多都具有相同的文件夹结构.大多数发行版只有用户空间标题而不是内核空间标题.

并且通常 make用于构建内核模块而不是裸机cc.按照这里Hello World给出的简单的逐步说明内核模块进行操作