您的位置:首页 > 运维架构 > Linux

Notes [ Linux Device Drivers, 3rd Edition ( Ch02 ) - The Hello World Module ]

2010-04-25 20:45 585 查看
很想接触一下Linux的内核,最近加入了LKML(Linux Kernel Mailing List),犹如潮水般的mail涌入了我的Gmail>_<

一开始很迷茫,如此庞大了Linux内核代码,从何开始?网上看到了一条建议:从Linux设备驱动的编写开始。

于是找到了一本这方面比较好的教材:Linux Device Drivers, 3rd Edition(LDD3)

不过这本书里面讲的第一个Hello world程序就不知道该如何编译,书里讲的也不是很详细,可能是认为读者已经有相关的知识了,可惜俺没有ToT

后来网上又找到了一篇文章,讲得比较详细,这里给个链接http://linux.chinaunix.net/bbs/viewthread.php?tid=904034&highlight=Godbach

接下来就是整个编译的过程

dolphin@dolphin-laptop:~/Documents/LDD$ gedit Makefile

dolphin@dolphin-laptop:~/Documents/LDD$ gedit hello.c

dolphin@dolphin-laptop:~/Documents/LDD$ make

make -C /lib/modules/2.6.31-21-generic/build M=/home/dolphin/Documents/LDD modules

make[1]: Entering directory `/usr/src/linux-headers-2.6.31-21-generic'

  CC [M]  /home/dolphin/Documents/LDD/hello.o

  Building modules, stage 2.

  MODPOST 1 modules

  CC      /home/dolphin/Documents/LDD/hello.mod.o

  LD [M]  /home/dolphin/Documents/LDD/hello.ko

make[1]: Leaving directory `/usr/src/linux-headers-2.6.31-21-generic'

dolphin@dolphin-laptop:~/Documents/LDD$ ls

hello.c   hello.ko     hello.mod.o  Makefile        modules.order

hello.c~  hello.mod.c  hello.o      Module.markers  Module.symvers

dolphin@dolphin-laptop:~/Documents/LDD$ sudo insmod ./hello.ko

[sudo] password for dolphin:

dolphin@dolphin-laptop:~/Documents/LDD$ lsmod

Module                  Size  Used by
hello                   1052  0

 

模块已经被加载了,不过没有该有字符串输出,不知道是怎么回事,也许是我没有用标准的内核,而是偷懒用了Ubuntu自带的内核

下面是hello.c和Makefile文件

dolphin@dolphin-laptop:~/Documents/LDD$ cat hello.c

#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 );

dolphin@dolphin-laptop:~/Documents/LDD$ cat Makefile

obj-m := hello.o

KERNELDIR := /lib/modules/2.6.31-21-generic/build

PWD := $(shell pwd)

modules:

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

modules_install:

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

clean:

    rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions

 

我知道为什么没有字符串输出了,刚才又仔细看了一遍LDD3,里面有这么一段话

According to the mechanism your system uses to deliver the message lines, your out-

put may be different. In particular, the previous screen dump was taken from a text

console; if you are running insmod and rmmod from a terminal emulator running

under the window system, you won’t see anything on your screen.

俺就是在Xwindows环境的gnome-terminal下运行的,难怪没有输出,后来(CTRL+ALT+F1)切换到CLI下去运行的时候,结果有输出了

dolphin@dolphin-laptop:~/Documents/LDD$ sudo insmod hello.ko

[ 8842.019793] Hello, world

dolphin@dolphin-laptop:~/Documents/LDD$ sudo rmmod hello

[8862.015773] Goodbye, cruel world

不过前面还多了串数字,不知道这是什么东东了,大概和时间有关吧~~暂且先放着,以后再研究了

接着看下去后貌似有点冤枉作者了,其实他在后面的“Compiling and Loading”一节中讲述了如何编写Makefile文件来编译模块的

# If KERNELRELEASE is defined, we've been invoked from the

# kernel build system and can use its language.

ifneq ($(KERNELRELEASE),)

     obj-m := hello.o

# Otherwise we were called directly from the command

# line; invoke the kernel build system.

else

     KERNELDIR ?= /lib/modules/$(shell uname -r)/build

     PWD := $(shell pwd)

default:

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

endif

不过他也说了这不是完整的Makefile,完整的Makefile应该包括清除一些不需要的文件,安装模块之类的。以下是此文章中的原文:

Do note that the above is not a complete makefile; a real makefile includes the usual sort of targets for cleaning up unneeded files, installing modules, etc. See the makefiles in the example source directory for a complete example.

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息