linux/kernel.h : 没有那个文件或目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5611254/
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
linux/kernel.h : No such file or directory
提问by
I am going to write a Hello World module in Ubuntu 10.10 (with the kernel 2.6.35-28-generic). Headers are located:
我将在 Ubuntu 10.10(内核为 2.6.35-28-generic)中编写一个 Hello World 模块。标头位于:
/usr/src/linux-headers-2.6.35-28-generic
hello.c:
你好ç:
#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 Makefile:
和生成文件:
CC = gcc
CFLAGS = -Wall -DMODULE -D__KERNEL__
hello.o: hello.c
$(CC) $(CFLAGS) -c hello.c
echo insmod hello.o to install
echo rmmod to delete
There is an error while make:
make 时出现错误:
hello.c:1: fatal error: linux/kernel.h : No such file or directory compilation terminated.
hello.c:1: 致命错误: linux/kernel.h : 没有这样的文件或目录编译终止。
How do I solve this?
我该如何解决这个问题?
采纳答案by Zuljin
Do you have /usr/src/linux symbolic link to your /usr/src/linux-headers-2.6.35-28-generic ? If not then create one using following commands
你有 /usr/src/linux 符号链接到你的 /usr/src/linux-headers-2.6.35-28-generic 吗?如果没有,则使用以下命令创建一个
# cd /usr/src
# ln -sfn linux-headers-2.6.35-28-generic linux
回答by Zitrax
For me this file ("linux/kernel.h") is in the package linux-libc-dev (Kubuntu 10.10).
对我来说,这个文件(“linux/kernel.h”)位于 linux-libc-dev(Kubuntu 10.10)包中。
回答by sarnold
You can't just use a traditional-style Makefile
with Linux kernel modules; while you mightbe able to force something to work, it'll be a painful experience.
你不能只使用传统风格Makefile
的 Linux 内核模块;虽然你可能能够强迫某些东西起作用,但这将是一种痛苦的经历。
Start by reading the Documentation/kbuild/modules.txt
file; it'll describe exactly what you need to do when writing a module Makefile
so that it can hook neatly into the kernel's Kbuild
environment. Your Makefile
will probably look something like this:
从读取Documentation/kbuild/modules.txt
文件开始;它将准确地描述在编写模块时您需要做什么,Makefile
以便它可以巧妙地挂钩到内核Kbuild
环境中。您Makefile
可能看起来像这样:
ifneq ($(KERNELRELEASE),)
# kbuild part of makefile
obj-m := 8123.o
8123-y := 8123_if.o 8123_pci.o 8123_bin.o
else
# normal makefile
KDIR ?= /lib/modules/`uname -r`/build
default:
$(MAKE) -C $(KDIR) M=$$PWD
# Module specific targets
genbin:
echo "X" > 8123_bin.o_shipped
endif
Please trust me on this; while you might think you're "just one small change" from getting your own Makefile
to work, even minor changes in kernel version will completely destroy your build all over again. Just take the hour now to write a Kbuild
-compatible Makefile
for your module. I wasted weeks of my life trying to maintain a pre-existing Makefile
when the Kbuild
infrastructure was introduced. Every new kernel caused me to lose hoursof productivity.
请相信我;虽然您可能认为自己Makefile
的工作“只是一个小小的改变” ,但即使内核版本中的微小更改也会完全破坏您的构建。现在只需花一个小时为您的模块编写一个Kbuild
-compatible Makefile
。Makefile
当Kbuild
基础设施被引入时,我浪费了我生命中的几个星期试图维持一个预先存在的东西。每一个新内核都让我失去了数小时的生产力。
回答by Sim Sun
just as what @sarnold said , you should use the different Makefile.Just as following:
正如@sarnold 所说,您应该使用不同的 Makefile.Just 如下:
obj-m += hello.o
all: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
obj-m += hello.o
全部:make -C /lib/modules/$(shell uname -r)/build M=$(PWD) 模块
and use the command:
并使用命令:
insmod hello.ko
insmod hello.ko
to install this module.
安装这个模块。