LinuxSir.cn,穿越时空的Linuxsir!

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

加载示例模块 helloworld 出错 ,大家帮我搞一下?3X

[复制链接]
发表于 2004-4-7 20:57:43 | 显示全部楼层 |阅读模式
示例模块代码如下:

---------------------------------------------------------
//It is copied from a book


#ifndef __KERNEL__
#define __KERNEL__
#endif
#ifndef MODULE
#define MODULE
#endif


#include </usr/src/linux-2.4.20-8/include/linux/kernel.h> /*We'ar doing kernel
work*/

#include </usr/src/linux-2.4.20-8/include/linux/module.h> /*Specifically a mod
ule*/



/*Deal with CONFIG_MODVERSIONS>*/



#if CONFIG_MODVERSIONS==1

#define <linux/modversions.h>

#endif



/*Initialize the module*/

int init_module()

{

        printk("Hello. World _ this is the kernel speaking\n");

       

        /* If we returned a non zero value, it means that

         * init_module failed and the kernel module can't be loaded*/



        return 0;

}



/*cleanup - undid whatever init_module did */

void cleanup_module()

{

        printk("Short is the file of the kernel module\n");

}
-------------------------------------------------------------

编译后加载,所有信息如下
[root@qg dd]# gcc -c helloword.c
In file included from /usr/src/linux-2.4.20-8/include/linux/module.h:25,
                 from helloword.c:11:
/usr/include/asm/atomic.h:40:2: warning: #warning Using kernel header in userl
and program. BAD!
[root@qg dd]# /sbin/insmod ./helloword.o
./helloword.o: kernel-module version mismatch
        ./helloword.o was compiled for kernel version 2.4.20
        while this kernel is version 2.4.20-8.

怎样做才能够加载成功啊?谢谢.
发表于 2004-4-8 00:33:39 | 显示全部楼层
insmod -f helloworld.o
insmod -f helloworld.o
或者编译内核为不支持版本控制功能
发表于 2004-4-9 20:04:11 | 显示全部楼层
偶编译的时候也遇到了这个问题,呜呜!
发表于 2004-4-10 13:46:10 | 显示全部楼层
将version.h中的版本号改成和uname -r中标出的一样就可以了
 楼主| 发表于 2004-4-12 03:00:26 | 显示全部楼层
[root@localhost myfirewall]# uname -r
2.4.22-10mdk

我把它改了,还是
helloag.o: couldn't find the kernel version the module was compiled for

( 我的系统已经换成mandrake 9.2了)
---------------------------------------------------
//helloag.c

//It is copied from a book

#include <linux/kernel.h> /*We'ar doing kernel work*/
#include <linux/module.h> /*Specifically a module*/

/*Deal with CONFIG_MODVERSIONS>*/

#if CONFIG_MODVERSIONS==1
#define <linux/modversions.h>
#endif

/*Initialize the module*/
int init_module()
{
        printk("Hello. World _ this is the kernel speaking\n");

        /* If we returned a non zero value, it means that
         * init_module failed and the kernel module can't be loaded*/

        return 0;
}

/*cleanup - undid whatever init_module did */
void cleanup_module()
{
        printk("Short is the file of the kernel module\n");
}
---------------------------------------------------------------
大哥,你的招数不行啊,唉...
不信试试?
 楼主| 发表于 2004-4-12 12:24:53 | 显示全部楼层
或者哪为仁兄直接给小弟写一段可以在2.4.22内核编译,加载的hello程序? 多谢了!
 楼主| 发表于 2004-4-12 20:14:02 | 显示全部楼层
首先在/usr/include/linux/version.h里把2.4.22改为2.4.22-10mdk
然后调整了头文件的次序:
--------------------------------------------------
#define MODULE
#include <linux/module.h>
#define __KERNEL__
#include <linux/kernel.h>
nt init_module(void) {
     printk("<1>Hello World\n");
     return 0;
}
void cleanup_module(void) {
     printk(KERN_ALERT "Goodbye world 1.\n");
}
------------------------------------------------------------

然后:
[root@localhost myfirewall]# gcc -c helloag.c
[root@localhost myfirewall]# /sbin/insmod helloag.o
Warning: loading helloag.o will taint the kernel: no license
  See http://www.tux.org/lkml/#export-tainted for information about tainted modules
Module helloag loaded, with warnings
[root@localhost myfirewall]# dmesg | tail -1
Hello World
[root@localhost myfirewall]# /sbin/rmmod helloag
[root@localhost myfirewall]# dmesg | tail -1
Goodbye world 1.
[root@localhost myfirewall]#

终于演示出来了!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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