Chinaunix首页 | 论坛 | 博客
  • 博客访问: 90946
  • 博文数量: 19
  • 博客积分: 442
  • 博客等级: 下士
  • 技术积分: 190
  • 用 户 组: 普通用户
  • 注册时间: 2011-07-11 15:31
文章分类
文章存档

2011年(19)

分类: LINUX

2011-07-29 14:33:57

东北人在上海

过着东北人的上海生活
document.getElementById("blogbrief").innerHTML="过着东北人的上海生活";
engelbert.cublog.cn 首页 | 文章 | 相册 | 收藏夹 | 留言


Linux 2.6版本模块编译入门
一. 准备工作
安装kernel 必须的开发库

#sudo apt-get install linux-kernel-devel

安装内核头文件

#sudo apt-get install linux-headers-`uname -r`

当然, gcc /make 等工具天生就是需要的。

二. 模块代码:

//------------------hello.c-------------------//


#ifndef __KERNEL__
#define __KERNEL__

#endif
#ifndef MODULE
#define MODULE
#endif

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>

static int hello_init(void)
{
      printk(KERN_ALERT "Hello World!\n");
        return 0;
}

static void hello_exit(void)
{
        printk(KERN_ALERT "Bye World!\n");
}

module_init(hello_init);
module_exit(hello_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Engelbert");

三.Makefile:

obj-m := hello.o
KDIR := /root/work/bs6040/kernel/linux-2.6.23/ #arm板上linux版本的源代码路径
KERNELBUILD :=/lib/modules/`uname -r`/build
default:
    make -C $(KDIR) M=$(shell pwd) modules
clean:
    rm -rf *.o *.ko *.mod.c .*.cmd .tmp_version

四. 编译模块

#sudo make

编译模块

这时,在hello.c 所在文件夹就会有 hello.ko ,这个就是我们需要的内核模块啦,哈哈!

清理编译垃圾,hello.ko 也会清理掉,呵呵。

五. 插入模块,让其工作

#sudo insmod ./hello.ko

我们用dmesg 就可以看到 产生的内核信息啦,Hello world!

#sudo rmmod ./hello

再用dmesg 可以看到 Bye world!

这就是在2.6.xx下一个最简单的模块编写过程。
创建于: 2008-06-18 21:24:34,修改于: 2008-06-23 14:10:35,已浏览423次,有评论0条


网友评论

发表评论
 昵称: 匿名
 
function $(s){return document.getElementById(s);} function check(){ if($("iscomment").value==0){ if($("username").value==""){ alert('请输入您的用户名!'); $("username").select(); return false; } if($("password").value==""){ alert('请输入您的密码!'); $("password").select(); return false; } }else{ if($("nickname").value==""){ alert('请输入您的称呼!'); $("nickname").select(); return false; } } if($("comment").value==""){ alert('你忘记输入内容了!'); $("comment").select(); return false; } return true; }
阅读(1489) | 评论(3) | 转发(1) |
给主人留下些什么吧!~~

华媛10022011-07-29 15:37:17

在使用命令ismod helloworld.ko 加载编译成功的模块helloworld.ko时出现错误  insmod: error inserting 'helloworld.ko': -1 Invalid module format
一般出错信息被记录在文件/var/log/messages中
[root@hailiang linux-2.6.15.5]# cat /var/log/messages |tail

  May  8 16:41:45 hailiang kernel: helloworld: version magic '2.6.27.5-117.fc10.i686 SMP mod_unload modversions 686 4KSTACKS ' should be '2.6.27.5-117.fc10.i686 SMP mod_unload 686 4KSTACKS '

通过命令看一下模块的相关信息

华媛10022011-07-29 15:28:32

新问题:
[root@firefly linuxmodule]# insmod ./hello.ko
insmod: error inserting './hello.ko': -1 Invalid module format

华媛10022011-07-29 15:21:43

补充:
仿写完成后。报错如下:
[tuxedo@firefly linuxmodule]$ make
make -C /bea/wangyy/mysrc/linuxmodule M=/bea/wangyy/mysrc/linuxmodule modules
make[1]: Entering directory `/bea/wangyy/mysrc/linuxmodule'
make[1]: *** No rule to make target `modules'.  Stop.
make[1]: Leaving directory `/bea/wangyy/mysrc/linuxmodule'
make: *** [default] Error 2

原因是自作聪明的把KDIR改成自己的目录了。
网上出现类似的都是没有安装linux内核源码的。

所以,老老实实如下:
obj-m :=