免费注册 查看新帖 |

Chinaunix

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

hello wrold 模块试验 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-02-24 14:57 |只看该作者 |倒序浏览
hello wrold 模块试验
        参考http://linux.chinaunix.net/bbs/thread-1042600-1-2.html
步骤:
1. 在任意路径下(最好拥有其权限)新建一目录test,用于存放我们的代码。
2. 在test目录下新建源代码文件hello.c,并敲入代码
               
               
                #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 "Goodbye, cruel world\n");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");
3. 在test目录下新建Makefile文件
obj-m := hello.o
      KERNELDIR := /lib/modules/2.6.28-11-generic/build
      
      PWD := $(shell pwd)
      
      
modules:
      
            $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
      
      modules_install:   
      
            $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
      
注:
        我这里KERNELDIR的值为 /lib/modules/2.6.28-11-generic/build[linux@ test]$ ll /lib/modules/2.6.28-11-generic/buildlrwxrwxrwx 1 root root 40 2009-12-05 08:47 /lib/modules/2.6.28-11-generic/build -> /usr/src/linux-headers-2.6.28-11-generic可以看到build文件实际上是软链接到/usr/src/linux-headers-2.6.28-11-generic目录的,该目录下存放的是linux内核头文件,且仍保持内核目录结构。
4. make 编译
在目录下生成数个文件
[linux@ test]$ lshello.c   hello.mod.c  hello.o   Module.markers  Module.symvershello.ko  hello.mod.o  Makefile  modules.order
其中的hello.ko便是我们用来加载的模块了
5. 加载模块sudo insmod ./hello.ko
查看信息[linux@ test]$ dmesg | tail -n 1[22588.694732] Hello, world!
6. 卸载模块[linux@ test]$ sudo rmmod hello[linux@ test]$ dmesg | tail -n 1[22665.554340] Goodbye, cruel world
小结:注意要保证Makefile文件中引用的头文件路径KERNELDIR变量值正确。      这里只是将试验的过程记录,至于对上面几行代码的解释,google


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/93566/showart_2185885.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP