我在 /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 文件 (M要大些)
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_驱动开发$
hello world驱动
最新推荐文章于 2021-07-29 08:59:40 发布