我想编译一个“hello world”内核模块的例子, 在ubuntu 11.04,kernel 3.2.6,gcc 4.5.2和fedora 16上发现的问题,kernel 3.2.7,gcc 4.6.7。模块编译:asm/linkage.h文件没有找到
代码:
#include <linux/module.h>
#include <linux/init.h>
MODULE_LICENSE("GPL");
static int __init hello_init (void)
{
printk("Hello module init\n");
return 0;
}
static void __exit hello_exit (void)
{
printk("Hello module exit\n");
}
module_init(hello_init);
module_exit(hello_exit);
编译:
gcc -D__KERNEL__ -I /usr/src/linux/include/ -DMODULE -Wall -O2 -c hello.c -o hello.o
错误:
In file included from /usr/src/linux/include/linux/kernel.h:13:0, from /usr/src/linux/include/linux/cache.h:4, from /usr/src/linux/include/linux/time.h:7, from /usr/src/linux/include/linux/stat.h:60, from /usr/src/linux/include/linux/module.h:10, from hello.c:1: /usr/src/linux/include/linux/linkage.h:5:25: fatal error: asm/linkage.h: file not found
后来我发现在/ usr/src/linux中/包括/没有文件夹命名'asm',但'asm-generic'; 所以我做了一个软链接“ASM”到“ASM-通用的”,并编译agail:
这一次的错误是:
In file included from /usr/src/linux/include/linux/preempt.h:9:0, from /usr/src/linux/include/linux/spinlock.h:50, from /usr/src/linux/include/linux/seqlock.h:29, from /usr/src/linux/include/linux/time.h:8, from /usr/src/linux/include/linux/stat.h:60, from /usr/src/linux/include/linux/module.h:10, from hello.c:1: /usr/src/linux/include/linux/thread_info.h:53:29: fatal error: asm/thread_info.h: file not found
于是我意识到我错了,但是为什么呢? T_T
删除我已经检查了3台机器内核的src文件夹(3.2+)与Ubuntu,Fedora和gentoo,它们都不包含文件夹'asm'。所以我认为这可能不是一个错误.. – 2012-02-29 08:17:12
在我检查过的Redhat中,'/ usr/src/kernels /.../include/asm'是一个到'asm-x86_64'的链接。 – ugoren 2012-02-29 10:43:49