免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2174 | 回复: 5
打印 上一主题 下一主题

请教一个很简单的模块加载问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-01-31 10:47 |只看该作者 |倒序浏览
初学模块化编程,试验一个最简单的hello.c模块加载,程序如下:
#define MODULE
#include <linux/module.h>

int init_modele(void)
{ printk("<1>Hello,world\n");
   return 0; }

void cleanup_module(void)
{ printk("<1>Goodbye world\n"); }

使用命令#gcc -c hello.c编译后生成hello.o
然后加载#insmod hello.o  
但是加载不上,说版本不匹配,hello.o was complied for kernel version 2.4.20, while this kernel  is version 2.4.20-8.
这有什么区别吗?怎么改就可以用了?

论坛徽章:
0
2 [报告]
发表于 2007-01-31 17:29 |只看该作者
gcc后面还该跟参数,用于指定用那个include
试试下面的:
*  
*  hello-1.c - The simplest kernel module.
*/
#include <linux/module.h>        /* Needed by all modules */
#include <linux/kernel.h>        /* Needed for KERN_ALERT */

int init_module(void)
{
        printk("<1>Hello world 1.\n");

        /*
         * A non 0 return means init_module failed; module can't be loaded.
         */
        return 0;
}

void cleanup_module(void)
{
        printk(KERN_ALERT "Goodbye world 1.\n");
}

(2)Makefile
obj-m += hello-1.o
(3)make -C /usr/src/linux-`uname -r` SUBDIRS=$PWD modules
(4)insmod ./hello-1.ko
(5)dmesg|tail
(6)rmmod hello-1

论坛徽章:
0
3 [报告]
发表于 2007-01-31 22:12 |只看该作者
从第二步开始都是在哪里做呢?我只会在终端里运行shell命令


原帖由 mudga 于 2007-1-31 17:29 发表于 2楼  
gcc后面还该跟参数,用于指定用那个include
试试下面的:
*  
*  hello-1.c - The simplest kernel module.
*/
#include <linux/module.h>        /* Needed by all modules */
#include <linux/ker ...

论坛徽章:
0
4 [报告]
发表于 2007-02-01 10:29 |只看该作者
原帖由 shanhuhai 于 2007-1-31 10:47 发表于 1楼  
初学模块化编程,试验一个最简单的hello.c模块加载,程序如下:
#define MODULE
#include <linux/module.h>

int init_modele(void)
{ printk("<1>Hello,world\n");
   return 0;  ...


这样试试:

gcc -O2 -Wall -D__KERNEL__ -DMODULE -I/usr/src/linux-2.4/include/  -c hello.c

论坛徽章:
0
5 [报告]
发表于 2007-02-01 12:59 |只看该作者
谢谢你,可以加载了,
但是为什么不打印出hello world消息呢?


原帖由 ld_jia 于 2007-2-1 10:29 发表于 4楼  


这样试试:

gcc -O2 -Wall -D__KERNEL__ -DMODULE -I/usr/src/linux-2.4/include/  -c hello.c

[ 本帖最后由 shanhuhai 于 2007-2-2 15:55 编辑 ]

论坛徽章:
0
6 [报告]
发表于 2007-02-03 16:26 |只看该作者
dmesg |tail
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP