ubuntu 字符设备驱动程序──hello world

我在 /home/shana/linux_q/ 目录下创建2个文本文件 hello.c Makefile

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

程序我就不解释了……

Makefile 文件

obj-m := hello.o
KERNELDIR := /lib/modules/2.6.20/build
PWD := $(shell pwd)

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

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

如果以上你都完成了在 make 时出现这样的错误

shana@shana:~/linux_驱动开发$ make
make: 没有什么可以做的为 `modules'。

原因很简单 你肯定是从我这直接复制的吧~~~呵呵,Makefile格式错误啦~
解决办法就是 你把如 $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install 移动到行首 然后按Tab 键自动对齐
这时里边的变量都变成绿色了~然后在 make 吧

shana@shana:~/linux_驱动开发$ make
make -C /lib/modules/2.6.22-14-generic/build M=/home/shana/linux_驱动开发 modules
make[1]: Entering directory `/usr/src/linux-headers-2.6.22-14-generic'
CC [M] /home/shana/linux_驱动开发/hello.o
Building modules, stage 2.
MODPOST 1 modules
CC /home/shana/linux_驱动开发/hello.mod.o
LD [M] /home/shana/linux_驱动开发/hello.ko
make[1]: Leaving directory `/usr/src/linux-headers-2.6.22-14-generic'
shana@shana:~/linux_驱动开发$


shana@shana:~/linux_驱动开发$ ls -l
总用量 124
-rw-r--r-- 1 shana shana 303 2008-03-16 10:43 hello.c
-rw-r--r-- 1 shana shana 49039 2008-03-16 12:11 hello.ko
-rw-r--r-- 1 shana shana 687 2008-03-16 12:11 hello.mod.c
-rw-r--r-- 1 shana shana 25840 2008-03-16 12:11 hello.mod.o
-rw-r--r-- 1 shana shana 24360 2008-03-16 12:11 hello.o
-rw-r--r-- 1 shana shana 8344 2008-03-16 09:17 linux_qudong_qu.txt
-rw-r--r-- 1 shana shana 266 2008-03-16 12:09 Makefile
-rw-r--r-- 1 shana shana 0 2008-03-16 12:11 Module.symvers
shana@shana:~/linux_驱动开发$


然后加载模块 (超级用户)

root@shana:/home/shana/linux_驱动开发# insmod ./hello.ko

按照书上的例子 会在终端显示 hello , world 但是运行后什么都没有出现 (原因不解)

root@shana:/home/shana/linux_驱动开发# insmod ./hello.ko
root@shana:/home/shana/linux_驱动开发#

查看加载模块

root@shana:/home/shana/linux_驱动开发# lsmod
Module Size Used by
hello 2560 0

已经加载上咯~~

删除模块

root@shana:/home/shana/linux_驱动开发# rmmod hello
root@shana:/home/shana/linux_驱动开发#

那程序的输出在那呢?书中说明 如果不出现在终端 则会写进 syslog 文件中

shana@shana:~/linux_驱动开发$ cat /var/log/syslog |grep world
Mar 16 12:14:53 shana kernel: [ 5937.529297] Hello, world
Mar 16 12:16:05 shana kernel: [ 6009.439036] Goodbye, cruel world
shana@shana:~/linux_驱动开发$

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值