linux/kernel.h :没有这样的文件或目录
我将在 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");
}
和 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
make 时出现错误:
hello.c:1:致命错误:linux/kernel.h:没有这样的文件或目录 编译终止。
我该如何解决这个问题?
I am going to write a Hello World module in Ubuntu 10.10 (with the kernel 2.6.35-28-generic). Headers are located:
/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:
hello.c:1: fatal error: linux/kernel.h : No such file or directory
compilation terminated.
How do I solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您不能只将传统风格的
Makefile
与 Linux 内核模块一起使用;虽然您也许能够强迫某些事情发挥作用,但这将是一次痛苦的经历。首先阅读
Documentation/kbuild/modules.txt
文件;它将准确描述您在编写模块Makefile
时需要做什么,以便它可以巧妙地挂接到内核的Kbuild
环境中。您的Makefile
可能如下所示:请相信我;虽然您可能认为您自己的
Makefile
工作“只是一个小变化”,但即使是内核版本的微小变化也会再次完全破坏您的构建。现在只需花一个小时为您的模块编写一个兼容Kbuild
的Makefile
即可。当引入Kbuild
基础设施时,我浪费了数周时间试图维护预先存在的Makefile
。每个新内核都会导致我损失几个小时的生产力。You can't just use a traditional-style
Makefile
with Linux kernel modules; while you might be able to force something to work, it'll be a painful experience.Start by reading the
Documentation/kbuild/modules.txt
file; it'll describe exactly what you need to do when writing a moduleMakefile
so that it can hook neatly into the kernel'sKbuild
environment. YourMakefile
will probably look something like this: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 aKbuild
-compatibleMakefile
for your module. I wasted weeks of my life trying to maintain a pre-existingMakefile
when theKbuild
infrastructure was introduced. Every new kernel caused me to lose hours of productivity.对我来说,这个文件(“linux/kernel.h”)位于 linux-libc-dev 包(Kubuntu 10.10)中。
For me this file ("linux/kernel.h") is in the package linux-libc-dev (Kubuntu 10.10).
您是否有 /usr/src/linux 符号链接到 /usr/src/linux-headers-2.6.35-28-generic ?
如果没有,则使用以下命令创建一个
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
正如@sarnold所说,你应该使用不同的Makefile。如下所示:
并使用以下命令:
安装此模块。
just as what @sarnold said , you should use the different Makefile.Just as following:
and use the command:
to install this module.