编译错误:linux/module.h:没有那个文件或目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10126134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
error compiling: linux/module.h: No such file or directory
提问by Uzair Farooq
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.
采纳答案by Pavan Manjunath
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 generallymake
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
并且通常make
用于构建内核模块而不是裸cc
. 按照此处Hello World
给出的简单分步解释内核模块进行操作
回答by ThiefMaster
You need the kernel headers; they are usually in /usr/include/
if installed.
你需要内核头文件;/usr/include/
如果安装,它们通常在。
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
.
除非您使用基于源代码的发行版或构建自己的内核,否则默认情况下不会安装它们;使用发行版的包管理器来安装它们。该包通常称为linux-headers
.
回答by hburde
Most Linux distros don't install kernel headers as default. Look for a package kernel-headers or something similar.
大多数 Linux 发行版默认不安装内核头文件。寻找一个包内核头文件或类似的东西。
回答by j?rgensen
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 notneeded (smart distros recognize this), and neither is /usr/include/whatever.
如果已经安装了内核,您需要内核构建环境(脚本、头文件和 Makefile 的选择)通常可以通过 /lib/modules/ version/build(指向它的符号链接)访问。否则,该目录是构建目录(System.map 所在的目录)。充满来源不是需要(智能发行版认识到这一点),也不是/ usr / include中/不管。
You also mustuse kbuild; calling cc -I
is not enough, and has not been for more than 10 years. You start off with a Kbuild
file:
您还必须使用 kbuild;打电话cc -I
是不够的,而且已经有10多年没有了。你从一个Kbuild
文件开始:
obj-m += mymodule.o
and a Makefile
:
和一个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
.
然后利用make
.
#defining __KERNEL__
and MODULE
is also pointless, because that is already set by kbuild if needed.
#defining __KERNEL__
andMODULE
也是没有意义的,因为如果需要,kbuild 已经设置了。
回答by Sudip Das
Source file name is basic.c
源文件名是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
现在为ubuntu制作文件
At first type on ur terminal that $(uname -r) then u will get the version.. that is using on ur system
首先在您的终端上输入 $(uname -r) 然后您将获得在您的系统上使用的版本
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