Hello World模块的编译和运行

#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");
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);

 一开始学习ldd3,就在Hello Wrold模块处卡住了。经过不断的查找资料和尝试,终于搞定了。现记录如下:

背景:操作系统: ubuntu10.04 
首先,建立内核树:
    1. 查看自己OS的内核版本: uname -r
           root@wyw-desktop:~# uname -r
           2.6.32-21-generic

    2. 下载内核源码:
        1.搜索源代码: apt-cache search linux-source
           root@wyw-desktop:~# apt-cache search linux-source
           linux-source - Linux kernel source with Ubuntu patches
           linux-source-2.6.32 - Linux kernel source for version 2.6.32 with Ubuntu patches
        2. 选择源代码包并下载: apt-get install linux-source-2.6.32(我选择的是linux-source-2.6.32)
        3. 下载完成后,在/usr/src下,有一个linux-source-2.6.32.tar.bz2,双击解压。
        4. 解压后生成一个新的目录/usr/src/linux-source-2.6.32,所有源代码都在该目录下
   3.切换到源代码目录下,配置内核,选择最快的原版的配置方式: make oldconfig
        root@wyw-desktop:/usr/src/linux-source-2.6.32# make oldconfig
        scripts/kconfig/conf -o arch/x86/Kconfig
        #
        # configuration written to .config
        #

   4. 然后make,这里要花1-2小时,耐心等吧
        root@wyw-desktop:/usr/src/linux-source-2.6.32# make
        执行完之后,可以看到在当前目录下生成一个新的文件,vmlinux
   5. 编译,安装模块:(不需要)
       root@wyw-desktop:/usr/src/linux-source-2.6.32#  make modules
       root@wyw-desktop:/usr/src/linux-source-2.6.32#  make modules_install
       执行结束后,会在/lib/modules下生成新的目录/lib/modules/2.6.32-21-generic
       到此,内核编译完成,内核树建立
 然后, 建立hello.c文件和Makefile

    1.编写Hello World模块源代码:

  2. 建立Makefile文件,输入以下内容:
 obj-m := hello.o
KERNELDIR := /usr/src/linux-headers-2.6.32-21-generic
PWD := $(shell pwd)

modules:
     $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
     $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install

最后,编译,运行模块并查看结果

  1. make,编译模块
      root@wyw-desktop:~/source/ldd3/hello# make
      make -C /usr/src/linux-headers-2.6.32-21-generic M=/root/source/ldd3/hello modules
      make[1]: 正在进入目录 `/usr/src/linux-headers-2.6.32-21-generic'
      CC [M]  /root/source/ldd3/hello/hello.o
      /root/source/ldd3/hello/hello.c:7:19: warning: unknown escape sequence '/N'
      Building modules, stage 2.
      MODPOST 1 modules
      CC      /root/source/ldd3/hello/hello.mod.o
      LD [M]  /root/source/ldd3/hello/hello.ko
      make[1]:正在离开目录 `/usr/src/linux-headers-2.6.32-21-generic'

  2. 加载模块: insmod ./hello.ko
        root@wyw-desktop:~/source/ldd3/hello# insmod ./hello.ko
  3. 查看正在运行的模块: lsmod
      root@wyw-desktop:~/source/ldd3/hello# lsmod
      Module                  Size  Used by
      hello                    680  0
      ...

  4. 查看模块输出,若终端未能看到模块的输出结果,则: cat /var/log/syslog
      root@wyw-desktop:~/source/ldd3/hello# cat /var/log/syslog
      Aug 21 12:53:25 wyw-desktop kernel: [ 7633.728990]  Hello,World
  5. 退出模块: rmmod hello

 

参考网页:http://forum.ubuntu.org.cn/viewtopic.php?f=97&t=109762
  
 

      

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值