如何编译驱动程序/内核模块以在 Linux Ubuntu 中使用?
只是用一些例子来扩展,问题如下: 给定以下源:
/* hello.c */
#include <linux/module.h>
#include <linux/kernel.h>
int init_module(void)
{
printk(KERN_INFO "Hello world\n");
return 0;
}
void cleanup_module(void)
{
printk(KERN_INFO "Goodbye world\n");
}
/* end of hello.c */
和以下 Makefile:
obj-m += hello.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
当我在源文件和 make 文件所在的目录中键入 make 时,我收到消息:“对‘all’无需执行任何操作” 编译似乎在这里停止并且没有创建目标文件。
现在为了测试,我尝试了一个新的更简单的 Makefile: 目标:=你好 ${TARGET}.o: ${TARGET}.c
运行 make 给出了新错误:hello.c:1: fatal error: linux/module.h: No such file or directory。
但是这个文件在以下文件夹中可用:
/usr/src/linux-headers-2.6.35-22/include/linux
以及
/usr/src/linux-headers-2.6.35-22-generic/include/linux
kernel.h 文件
中我缺少什么,有什么想法吗?
提前致谢
Just to expand with some examples, here is the problem:
Given the following source:
/* hello.c */
#include <linux/module.h>
#include <linux/kernel.h>
int init_module(void)
{
printk(KERN_INFO "Hello world\n");
return 0;
}
void cleanup_module(void)
{
printk(KERN_INFO "Goodbye world\n");
}
/* end of hello.c */
and the following Makefile:
obj-m += hello.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
When I type make while in the same directory as the source and make files, I get the message: 'Nothing to be done for `all'
The compilation seems to stop here and no object file is created.
Now just for testing, I tried a new simpler Makefile:
TARGET := hello
${TARGET}.o: ${TARGET}.c
Running make gives me the new error:hello.c:1: fatal error: linux/module.h: No such file or directory.
however this file IS available in the folder:
/usr/src/linux-headers-2.6.35-22/include/linux
and also in
/usr/src/linux-headers-2.6.35-22-generic/include/linux
as is the kernel.h file
What am I missing, any ideas?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
确保 Makefile 中以“make”开头的行用制表符缩进。
即“all:”下面的行和“clean:”下面的行应该以制表符开头。
Make sure the lines beginning with 'make' in your Makefile are indented by a tab character.
That is the line below 'all:' and the line below 'clean:' should begin with a tab.