LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1846|回复: 16

最简单的一个内核模块 编译不通过???

[复制链接]
发表于 2005-5-4 09:46:32 | 显示全部楼层 |阅读模式
刚开始学习内核,  我的模块文件hello.c
/*
*      Hello world module.
*/
#include <linux/module.h>

#if defined(CONFIG_SMP)
#define __SMP__
#endif

#if defined(CONFIG_MODVERSIONS)
#define MODVERSIONS
#include <linux/modversions.h>
#endif

#include <linux/kernel.h>

int init_module(void)
{
        printk(KERN_DEBUG "Hello, kernel!\n");
        return 0;
}

void cleanup_module(void)
{
        printk(KERN_DEBUG "Good-bye, kernel!\n");
}


编译明令gcc -D__KERNEL__ -I/usr/include -DMODULE -Wall -O2   -c -o hello.o hello.c

结果报错:In file included from /usr/include/linux/module.h:10,
                 from hello.c:4:
/usr/include/linux/config.h:5:2: #error Incorrectly using glibc headers for a kernel module
hello.c: In function `init_module':
hello.c:19: warning: implicit declaration of function `printk'
hello.c:19: error: `KERN_DEBUG' undeclared (first use in this function)
hello.c:19: error: (Each undeclared identifier is reported only once
hello.c:19: error: for each function it appears in.)
hello.c:19: error: syntax error before string constant
hello.c: In function `cleanup_module':
hello.c:25: error: `KERN_DEBUG' undeclared (first use in this function)
hello.c:25: error: syntax error before string constant

请问什么原因,谢谢
发表于 2005-5-4 22:00:53 | 显示全部楼层
我是个菜鸟,只是正巧今天也刚遇到和你一样的问题

你的问题可能是编译命令中库的位置不对,你试试?!

另外,请各位高手不要鄙视我们初学者的“弱智”问题,能帮一下就帮一下了,干吗只看不回?这些对你们都很easy
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-5-6 08:24:02 | 显示全部楼层
求救 大家帮帮忙 我是照着书写的 可能是书比较老 我用fc2 是不是什么地方要改
回复 支持 反对

使用道具 举报

发表于 2005-5-6 11:18:01 | 显示全部楼层
可能是include设置错误,
-I/usr/src/linux**/include
**代表你的内核源码主目录
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-5-7 07:51:52 | 显示全部楼层
是这样 那么 fc2默认的内核源码主目录在哪里呢 我的那个不对么
回复 支持 反对

使用道具 举报

发表于 2005-5-7 17:28:22 | 显示全部楼层
Post by lmszcm
是这样 那么 fc2默认的内核源码主目录在哪里呢 我的那个不对么

你自己看看/usr/src下是不是有linux的源代码?
很多发行版本默认都不安装源代码的。
回复 支持 反对

使用道具 举报

发表于 2005-5-8 02:44:46 | 显示全部楼层
FC2使用2.6.5内核,
2.6与2.4内核模块编程方式有很大区别!
请参考:
感染2.6内核模块
http://www.gd-linux.org/bbs/archive/index.php/t-1091.html
回复 支持 反对

使用道具 举报

发表于 2005-5-8 09:41:21 | 显示全部楼层
小弟不才,曾写过一个编译2.6模块的帖子,希望对你有帮助,高手千万别笑啊~
http://www.linuxfans.org/nuke/mo ... wtopic&t=114089
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-5-9 08:21:51 | 显示全部楼层
我在安装盘Fedora/RPMS里找到一个叫kernel-source-2.6.5-1.358.i386.rpm的文件,请问这个是不是源码包,可是我安装不上,不知为什么,总说有些依赖文件没有,
回复 支持 反对

使用道具 举报

发表于 2005-5-9 11:13:43 | 显示全部楼层
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表